handle_net.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. package mokuai
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/tidwall/gjson"
  8. "golang.org/x/net/proxy"
  9. "io"
  10. "io/ioutil"
  11. "math/big"
  12. "net"
  13. "net/http"
  14. "net/url"
  15. "os"
  16. "strings"
  17. "time"
  18. )
  19. // Get ,失败返回空
  20. func Net_UrlGet(url string) string {
  21. // 超时时间:20秒
  22. client := &http.Client{Timeout: 20 * time.Second}
  23. resp, err := client.Get(url)
  24. if err != nil {
  25. panic(err)
  26. return ""
  27. }
  28. defer func() {
  29. if e := recover(); e != nil {
  30. fmt.Printf("Panicing %s\r\n", e)
  31. }
  32. resp.Body.Close()
  33. }()
  34. var buffer [512]byte
  35. result := bytes.NewBuffer(nil)
  36. for {
  37. n, err := resp.Body.Read(buffer[0:])
  38. result.Write(buffer[0:n])
  39. if err != nil && err == io.EOF {
  40. break
  41. } else if err != nil {
  42. panic(err)
  43. }
  44. }
  45. return result.String()
  46. }
  47. // Get ,返回字节集 ,失败返回空. outtime 超时时间(s)
  48. func Net_UrlGetByte(url string, outtime int) []byte {
  49. // 默认超时时间:5秒
  50. if outtime < 1 {
  51. outtime = 5
  52. }
  53. client := &http.Client{Timeout: time.Duration(outtime) * time.Second}
  54. resp, err := client.Get(url)
  55. if err != nil {
  56. panic(err)
  57. return []byte("")
  58. }
  59. defer func() {
  60. if e := recover(); e != nil {
  61. fmt.Printf("Panicing %s\r\n", e)
  62. }
  63. resp.Body.Close()
  64. }()
  65. var buffer [512]byte
  66. result := bytes.NewBuffer(nil)
  67. for {
  68. n, err := resp.Body.Read(buffer[0:])
  69. result.Write(buffer[0:n])
  70. if err != nil && err == io.EOF {
  71. break
  72. } else if err != nil {
  73. panic(err)
  74. }
  75. }
  76. return result.Bytes()
  77. }
  78. // Post ,返回文本型 。请求data为json格式
  79. func Net_UrlPostJson_Str(url string, data interface{}, contentType string) string {
  80. // 超时时间:10秒
  81. client := &http.Client{Timeout: 10 * time.Second}
  82. jsonStr, _ := json.Marshal(data)
  83. resp, err := client.Post(url, contentType, bytes.NewBuffer(jsonStr))
  84. if err != nil {
  85. panic(err)
  86. }
  87. defer func() {
  88. if e := recover(); e != nil {
  89. fmt.Printf("Panicing %s\r\n", e)
  90. }
  91. resp.Body.Close()
  92. }()
  93. // _ = resp.Body.Close()
  94. result, _ := ioutil.ReadAll(resp.Body)
  95. return string(result)
  96. }
  97. // Post ,返回文本型 。请求data为普通data
  98. func Net_UrlPostStr_Str(url string, data string, contentType string) string {
  99. // 超时时间:10秒
  100. client := &http.Client{Timeout: 10 * time.Second}
  101. resp, err := client.Post(url, contentType, bytes.NewBuffer([]byte(data)))
  102. if err != nil {
  103. panic(err)
  104. }
  105. defer func() {
  106. if e := recover(); e != nil {
  107. fmt.Printf("Panicing %s\r\n", e)
  108. }
  109. resp.Body.Close()
  110. }()
  111. // _ = resp.Body.Close()
  112. result, _ := ioutil.ReadAll(resp.Body)
  113. return string(result)
  114. }
  115. // Post ,返回字节集 。 失败返回空字节集
  116. func Net_UrlPostJson_Byte(url string, data interface{}, contentType string) []byte {
  117. // 超时时间:20秒
  118. client := &http.Client{Timeout: 20 * time.Second}
  119. jsonStr, _ := json.Marshal(data)
  120. resp, err := client.Post(url, contentType, bytes.NewBuffer(jsonStr))
  121. if err != nil {
  122. panic(err)
  123. return []byte("")
  124. }
  125. defer func() {
  126. if e := recover(); e != nil {
  127. fmt.Printf("Panicing %s\r\n", e)
  128. }
  129. resp.Body.Close()
  130. }()
  131. result, _ := ioutil.ReadAll(resp.Body)
  132. return result
  133. }
  134. // Post ,返回字节集 。请求data为普通data
  135. func Net_UrlPostStr_Byte(url string, data string, contentType string) []byte {
  136. // 超时时间:20秒
  137. client := &http.Client{Timeout: 20 * time.Second}
  138. resp, err := client.Post(url, contentType, bytes.NewBuffer([]byte(data)))
  139. if err != nil {
  140. panic(err)
  141. }
  142. defer func() {
  143. if e := recover(); e != nil {
  144. fmt.Printf("Panicing %s\r\n", e)
  145. }
  146. resp.Body.Close()
  147. }()
  148. result, _ := ioutil.ReadAll(resp.Body)
  149. return result
  150. }
  151. // 获取指定网卡名的网卡ip , 只输出ipv4 ,没有返回空。 返回格式 xx.xx.xx.xx/xx
  152. func Net_GetIPmaskByName(intername string) string {
  153. byName, _ := net.InterfaceByName(intername)
  154. addresses, _ := byName.Addrs()
  155. for _, vv := range addresses {
  156. if strings.IndexAny(vv.String(), ".") != -1 {
  157. return vv.String()
  158. }
  159. }
  160. return ""
  161. }
  162. // 获取指定网卡名的网卡ip , 只输出ipv4 ,没有返回空。 返回格式 xx.xx.xx.xx
  163. func Net_GetIPByName(intername string) string {
  164. byName, _ := net.InterfaceByName(intername)
  165. addresses, _ := byName.Addrs()
  166. for _, vv := range addresses {
  167. if strings.IndexAny(vv.String(), ".") != -1 {
  168. comma := strings.Index(vv.String(), "/")
  169. return vv.String()[:comma]
  170. }
  171. }
  172. return ""
  173. }
  174. // 把Post请求过来的 data 输出显示
  175. func Net_DebugPostData(c *gin.Context) {
  176. data, _ := c.GetRawData()
  177. fmt.Printf("Post Data: %v\n", string(data))
  178. c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(data))
  179. }
  180. // 获取本服务器IP
  181. func Net_Getmyfwqip() (ipwai, ipnei string) {
  182. addrs, err := net.InterfaceAddrs()
  183. if err != nil {
  184. fmt.Println("Net_Getmyfwqip Err:", err)
  185. os.Exit(1)
  186. }
  187. for _, address := range addrs {
  188. // 检查ip地址判断是否回环地址
  189. if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
  190. if ipnet.IP.To4() != nil {
  191. // fmt.Println("ip:", ipnet.IP.String())
  192. if Net_IsPublicIP(ipnet.IP) == true {
  193. // fmt.Println("公网IP:"+ipnet.IP.String())
  194. ipwai = ipnet.IP.String()
  195. } else if Net_Isothernet(ipnet.IP) == false {
  196. // fmt.Println("内网IP:"+ipnet.IP.String())
  197. ipnei = ipnet.IP.String()
  198. } else {
  199. // fmt.Println("Open IP:"+ipnet.IP.String())
  200. }
  201. }
  202. }
  203. }
  204. return
  205. }
  206. // 判断是否为公网ip
  207. func Net_IsPublicIP(IP net.IP) bool {
  208. if IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() {
  209. return false
  210. }
  211. if ip4 := IP.To4(); ip4 != nil {
  212. switch true {
  213. case ip4[0] == 10:
  214. return false
  215. case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
  216. return false
  217. case ip4[0] == 192 && ip4[1] == 168:
  218. return false
  219. default:
  220. return true
  221. }
  222. }
  223. return false
  224. }
  225. // 判断是否是open/169网段
  226. func Net_Isothernet(IP net.IP) bool {
  227. if ip4 := IP.To4(); ip4 != nil {
  228. switch true {
  229. case ip4[0] == 150:
  230. if ip4[1] == 33 {
  231. return true
  232. } else {
  233. return false
  234. }
  235. case ip4[0] == 169:
  236. if ip4[1] == 254 {
  237. return true
  238. } else {
  239. return false
  240. }
  241. default:
  242. return false
  243. }
  244. }
  245. return true
  246. }
  247. // 获取本服务器IP_接口方式 主海外
  248. func Net_Getmyip_api() (ip, area string) {
  249. s := Net_UrlGet("http://myip.zzznb.cc")
  250. ip = gjson.Get(s, "ip").Str
  251. if ip == "" {
  252. res, _ := http.Get("http://tom.myip.top")
  253. s, _ := ioutil.ReadAll(res.Body)
  254. ip = gjson.Get(string(s), "ip").Str
  255. }
  256. if ip == "" {
  257. res, _ := http.Get("http://ky.myip.top")
  258. s, _ := ioutil.ReadAll(res.Body)
  259. ip = gjson.Get(string(s), "ip").Str
  260. }
  261. area = gjson.Get(s, "country").Str + " " + gjson.Get(s, "province").Str + " " + gjson.Get(s, "city").Str + " " + gjson.Get(s, "isp").Str
  262. return
  263. }
  264. /*
  265. 使用HTTP代理GET访问(可以设置超时时间)
  266. urlstr 要访问的URL
  267. prxyurl 代理服务器地址
  268. outtime 超时时长,单位 秒
  269. 返回
  270. body:接收的数据
  271. err:错误信息
  272. */
  273. func Net_UrlProxyGet(urlstr, proxy string, outtime int) (body []byte, err error) {
  274. urli := url.URL{}
  275. var proxylin string
  276. if strings.Index(proxy, "http://") == -1 {
  277. proxylin = "http://" + proxy
  278. } else {
  279. proxylin = proxy
  280. }
  281. urlproxy, _ := urli.Parse(proxylin)
  282. client := &http.Client{
  283. Timeout: time.Second * time.Duration(outtime),
  284. Transport: &http.Transport{
  285. Proxy: http.ProxyURL(urlproxy),
  286. },
  287. }
  288. var rqt *http.Request
  289. rqt, err = http.NewRequest("GET", urlstr, nil)
  290. if err != nil {
  291. return
  292. }
  293. // rqt.Header.Add("User-Agent", "xxx")
  294. //处理返回结果
  295. var response *http.Response
  296. response, err = client.Do(rqt)
  297. if response != nil {
  298. defer response.Body.Close()
  299. }
  300. if err != nil {
  301. return []byte(""), err
  302. }
  303. body, err = ioutil.ReadAll(response.Body)
  304. defer func() {
  305. if e := recover(); e != nil {
  306. fmt.Printf("Panicing %s\r\n", e)
  307. }
  308. }()
  309. return
  310. }
  311. /*
  312. 使用Socks5代理GET访问
  313. urlstr 要访问的URL
  314. outtime 超时时长,单位 秒
  315. prxyurl 代理服务器地址
  316. 返回
  317. body:接收的数据
  318. err:错误信息
  319. */
  320. func Net_UrlProxyS5Get(urlstr string, outtime int, prxyurl, user, passwd string) (body []byte, err error) {
  321. //urltmp, _ := url.Parse(prxyurl)
  322. var authtmp proxy.Auth
  323. authtmp.User = user
  324. authtmp.Password = passwd
  325. dialer, err := proxy.SOCKS5("tcp", prxyurl, &authtmp, proxy.Direct) //开启socks5连接
  326. if err != nil {
  327. //fmt.Println("socks5连接出错%v", err)
  328. return
  329. }
  330. client := &http.Client{
  331. Timeout: time.Second * time.Duration(outtime),
  332. Transport: &http.Transport{
  333. Dial: dialer.Dial,
  334. },
  335. }
  336. var rqt *http.Request
  337. rqt, err = http.NewRequest("GET", urlstr, nil)
  338. if err != nil {
  339. return
  340. }
  341. // rqt.Header.Add("User-Agent", "xxx")
  342. //处理返回结果
  343. var response *http.Response
  344. response, err = client.Do(rqt)
  345. if response != nil {
  346. defer response.Body.Close()
  347. }
  348. defer func() {
  349. if e := recover(); e != nil {
  350. fmt.Printf("Panicing %s\r\n", e)
  351. }
  352. }()
  353. if err != nil {
  354. return
  355. }
  356. body, err = ioutil.ReadAll(response.Body)
  357. return
  358. }
  359. /*
  360. 使用HTTP代理POST访问(可以设置超时时间)
  361. urlstr 要访问的URL
  362. data POST数据
  363. prxyurl 代理服务器地址
  364. outtime 超时时长,单位 秒
  365. headers 协议头 如 headers := make(map[string]string)
  366. headers["Content-Type"] = "application/json;charset=utf-8"
  367. headers["token"] = token
  368. headers["Connection"] = "keep-alive"
  369. headers["Accept"] = "*\/*" 去掉\
  370. headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
  371. headers["X-Requested-With"] = "XMLHttpRequest"
  372. headers["Referer"] = "http://www.xxxx.com/"
  373. 返回
  374. body:接收的数据
  375. err:错误信息
  376. Recookies 返回的Cookies
  377. */
  378. func Net_UrlProxyPost(urlstr, data, proxy string, outtime int, headers map[string]string) (body []byte, err error, Recookies []*http.Cookie) {
  379. urli := url.URL{}
  380. var proxylin string
  381. if strings.Index(proxy, "http://") == -1 {
  382. proxylin = "http://" + proxy
  383. } else {
  384. proxylin = proxy
  385. }
  386. urlproxy, _ := urli.Parse(proxylin)
  387. client := &http.Client{
  388. Timeout: time.Second * time.Duration(outtime),
  389. Transport: &http.Transport{
  390. Proxy: http.ProxyURL(urlproxy),
  391. },
  392. }
  393. var rqt *http.Request
  394. rqt, err = http.NewRequest("POST", urlstr, bytes.NewReader([]byte(data)))
  395. if err != nil {
  396. return
  397. }
  398. for key, header := range headers {
  399. rqt.Header.Set(key, header)
  400. }
  401. // rqt.Header.Add("User-Agent", "xxx")
  402. //处理返回结果
  403. var response *http.Response
  404. response, err = client.Do(rqt)
  405. if response != nil {
  406. defer response.Body.Close()
  407. }
  408. defer func() {
  409. if e := recover(); e != nil {
  410. fmt.Printf("Panicing %s\r\n", e)
  411. }
  412. }()
  413. if err != nil {
  414. return []byte(""), err, nil
  415. }
  416. Recookies = response.Cookies()
  417. body, err = ioutil.ReadAll(response.Body)
  418. return
  419. }
  420. /*
  421. 使用HTTP代理GET访问 功能扩展版(可以设置超时时间、cookies、协议头)
  422. urlstr 要访问的URL
  423. prxyurl 代理服务器地址
  424. outtime 超时时长,单位 秒
  425. cookies 请求使用的Cookies,
  426. headers 协议头 如 headers := make(map[string]string)
  427. headers["Content-Type"] = "application/json;charset=utf-8"
  428. headers["token"] = token
  429. 返回
  430. body:接收的数据
  431. err:错误信息
  432. */
  433. func Net_UrlProxyGet_EX(urlstr, proxy string, cookies []*http.Cookie, outtime int, headers map[string]string) (body []byte, err error, Recookies []*http.Cookie) {
  434. urli := url.URL{}
  435. var proxylin string
  436. if strings.Index(proxy, "http://") == -1 {
  437. proxylin = "http://" + proxy
  438. } else {
  439. proxylin = proxy
  440. }
  441. urlproxy, _ := urli.Parse(proxylin)
  442. client := &http.Client{
  443. Timeout: time.Second * time.Duration(outtime),
  444. Transport: &http.Transport{
  445. Proxy: http.ProxyURL(urlproxy),
  446. ResponseHeaderTimeout: time.Second * time.Duration(outtime),
  447. },
  448. }
  449. var rqt *http.Request
  450. rqt, err = http.NewRequest("GET", urlstr, nil)
  451. if err != nil {
  452. fmt.Println("Get -2 >", err.Error())
  453. return []byte(""), err, nil
  454. }
  455. if cookies != nil {
  456. for i := 0; i < len(cookies); i++ {
  457. rqt.AddCookie(cookies[i])
  458. }
  459. }
  460. if headers != nil {
  461. for key, header := range headers {
  462. rqt.Header.Set(key, header)
  463. }
  464. }
  465. // rqt.Header.Add("User-Agent", "xxx")
  466. //处理返回结果
  467. var response *http.Response
  468. response, err = client.Do(rqt)
  469. if response != nil {
  470. defer response.Body.Close()
  471. }
  472. if err != nil {
  473. fmt.Println("Get -3 >", err.Error())
  474. return
  475. } else {
  476. Recookies = response.Cookies()
  477. // defer response.Body.Close()
  478. body, err = ioutil.ReadAll(response.Body)
  479. }
  480. defer func() {
  481. if e := recover(); e != nil {
  482. fmt.Printf("Panicing %s\r\n", e)
  483. }
  484. }()
  485. return
  486. }
  487. /*// Cookies转换, string 转 []*cookie
  488. func Net_CookieStrToCook(str string) (CookieZu []*http.Cookie) {
  489. s1 := strings.Split(str, ";")
  490. for i,v := range s1 {
  491. comma := strings.Index(str, ";")
  492. name := str[:comma]
  493. value := str[comma+len(";"):]
  494. c := &http.Cookie{
  495. Name: name,
  496. Value: value,
  497. Raw: line,
  498. }
  499. if name != "" {
  500. CookieZu
  501. }
  502. }
  503. }*/
  504. // Cookies转换, []*cookie 转 string
  505. func Net_CookieCookToStr(CookieZu []*http.Cookie) string {
  506. lin := ""
  507. for _, v := range CookieZu {
  508. if v.Raw != "" {
  509. lin = lin + v.Raw + ";"
  510. }
  511. }
  512. return lin
  513. }
  514. /*
  515. Net_Url_EX
  516. 使用HTTP访问(可以设置代理/超时时间/Cookie/请求类型/协议头等) . V 1.0.2 2022.4.8
  517. methed [选]
  518. urlstr [必]要访问的URL
  519. data [选]POST数据
  520. proxy [选]代理服务器地址 . 为空则不使用代理 。 账密代理格式 {proxyUrl:proxyPort}@{proxyUser}:{proxyPass} 例如 127.0.0.1:8888@user:password; 仅代理格式 {proxyUrl:proxyPort} 例如 127.0.0.1:8888
  521. cookies [选]传入访问使用的Cookies . 为空则不使用
  522. outtime [选]超时时长,单位 秒 ,默认6秒
  523. headers [选]协议头 如 headers := make(map[string]string)
  524. headers["Content-Type"] = "application/json;charset=utf-8"
  525. headers["token"] = token
  526. headers["Connection"] = "keep-alive"
  527. headers["Accept"] = "*\/*" 去掉\
  528. headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
  529. headers["X-Requested-With"] = "XMLHttpRequest"
  530. headers["Referer"] = "http://www.xxxx.com/"
  531. autoredirect [选]是否自动重定向, 1:自动处理 ; 2.不处理 ; 默认为0:自动处理
  532. 返回
  533. body:接收的数据 , (字符串方式 string(body))
  534. err:错误信息
  535. Recookies 返回的Cookies 。 可以配合 Net_CookieCookToStr 使用
  536. */
  537. func Net_Url_EX(methed, urlstr, data, proxy, cookies string, outtime int, headers map[string]string, autoredirect int) (body []byte, err error, ReCookies string, ReHeader http.Header) {
  538. if methed == "" || len(methed) < 2 {
  539. methed = "GET"
  540. }
  541. if outtime < 1 {
  542. outtime = 6
  543. }
  544. var client *http.Client
  545. if proxy == "" {
  546. client = &http.Client{
  547. Timeout: time.Second * time.Duration(outtime),
  548. }
  549. } else {
  550. urli := url.URL{}
  551. var proxyUrl, proxyUser, proxyPass string
  552. if !strings.Contains(proxy, "http") {
  553. proxy = fmt.Sprintf("http://%s", proxy)
  554. }
  555. if strings.Index(proxy, "@") == -1 { // 仅代理URL方式
  556. proxyUrl = proxy
  557. } else {
  558. comma := strings.Index(proxy, "@")
  559. proxyUrl = proxy[:comma]
  560. lin := proxy[comma+len("@"):]
  561. if len(lin) > 0 {
  562. if strings.Index(lin, ":") == -1 {
  563. proxyUser = lin
  564. proxyPass = ""
  565. } else {
  566. comma = strings.Index(lin, ":")
  567. proxyUser = lin[:comma]
  568. proxyPass = lin[comma+len(":"):]
  569. }
  570. }
  571. }
  572. urlProxy, _ := urli.Parse(proxyUrl)
  573. if proxyUser != "" && proxyPass != "" {
  574. urlProxy.User = url.UserPassword(proxyUser, proxyPass)
  575. }
  576. client = &http.Client{
  577. Timeout: time.Second * time.Duration(outtime),
  578. Transport: &http.Transport{
  579. Proxy: http.ProxyURL(urlProxy),
  580. },
  581. }
  582. }
  583. var rqt *http.Request
  584. rqt, err = http.NewRequest(methed, urlstr, bytes.NewReader([]byte(data)))
  585. if err != nil {
  586. return
  587. }
  588. if len(headers) == 0 { // 判断是否需要自动补充基本协议头
  589. rqt.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36")
  590. } else {
  591. for key, header := range headers {
  592. rqt.Header.Set(key, header)
  593. }
  594. }
  595. if len(cookies) > 1 {
  596. rqt.Header.Add("Cookie", cookies)
  597. }
  598. if autoredirect == 2 { // 处理重定向使用
  599. client.CheckRedirect = func(rqt *http.Request, via []*http.Request) error {
  600. return http.ErrUseLastResponse
  601. }
  602. }
  603. // rqt.Header.Add("User-Agent", "xxx")
  604. //处理返回结果
  605. var response *http.Response
  606. response, err = client.Do(rqt)
  607. if response != nil {
  608. defer func(Body io.ReadCloser) {
  609. err := Body.Close()
  610. if err != nil {
  611. fmt.Println("client.Do Err > " + err.Error())
  612. }
  613. }(response.Body)
  614. }
  615. defer func() {
  616. if e := recover(); e != nil {
  617. fmt.Printf("Panicing %s\r\n", e)
  618. }
  619. }()
  620. if err != nil {
  621. return []byte(""), err, "", nil
  622. }
  623. cooklin := response.Cookies()
  624. for _, v := range cooklin {
  625. ReCookies = ReCookies + v.Raw + ";"
  626. }
  627. ReHeader = response.Header.Clone()
  628. body, err = ioutil.ReadAll(response.Body)
  629. return
  630. }
  631. // cookies 合并更新,返回合并后的Cookies
  632. func Net_CookieAndUp(oldCookies, newCookies string) string {
  633. linzu := strings.Split(oldCookies, ";")
  634. linzu1 := strings.Split(newCookies, ";")
  635. lin := ""
  636. for _, v := range linzu {
  637. n := -1
  638. comma := strings.Index(v, "=")
  639. for i1, v1 := range linzu1 {
  640. if strings.Index(v, "=") != -1 && strings.Index(v1, "=") != -1 {
  641. comma1 := strings.Index(v1, "=")
  642. if v[:comma] == v1[:comma1] {
  643. lin = lin + v1 + ";"
  644. linzu1[i1] = ""
  645. n = 1
  646. break
  647. }
  648. }
  649. }
  650. if n == -1 {
  651. lin = lin + v
  652. }
  653. }
  654. for _, v := range linzu1 {
  655. if v != "" {
  656. lin = lin + v + ";"
  657. }
  658. }
  659. return lin
  660. }
  661. // 获取URL中的域名 (非根域名)
  662. func Net_GetDomain(url string) (domain string) {
  663. var lin string
  664. if strings.Index(url, "http://") != -1 {
  665. comma := strings.Index(url, "http://")
  666. lin = url[comma+len("http://"):]
  667. } else if strings.Index(url, "https://") != -1 {
  668. comma := strings.Index(url, "https://")
  669. lin = url[comma+len("https://"):]
  670. } else {
  671. lin = url
  672. }
  673. if strings.Index(lin, "/") != -1 {
  674. comma := strings.Index(lin, "/")
  675. lin = lin[:comma]
  676. }
  677. // 过滤掉端口
  678. if strings.Index(lin, ":") != -1 {
  679. comma := strings.Index(lin, ":")
  680. lin = lin[:comma]
  681. }
  682. return lin
  683. }
  684. // IP长整数型转字符串IP
  685. func Net_IPNtoA(ip int64) string {
  686. return fmt.Sprintf("%d.%d.%d.%d",
  687. byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip))
  688. }
  689. // IP字符串型转长整数IP
  690. func Net_IPAtoN(ip string) int64 {
  691. ret := big.NewInt(0)
  692. ret.SetBytes(net.ParseIP(ip).To4())
  693. return ret.Int64()
  694. }
  695. // Net_downloadFile 会将url下载到本地文件,它会在下载时写入,而不是将整个文件加载到内存中。
  696. // url 文件下载的网址
  697. // filepath 文件保存的路径,包含文件名
  698. func Net_downloadFile(url string, filepath string) (bool, error) {
  699. // Get the data
  700. resp, err := http.Get(url)
  701. if err != nil {
  702. return false, err
  703. }
  704. defer resp.Body.Close()
  705. // Create output file
  706. out, err := os.Create(filepath)
  707. if err != nil {
  708. return false, err
  709. }
  710. defer out.Close()
  711. // copy stream
  712. _, err = io.Copy(out, resp.Body)
  713. if err != nil {
  714. return false, err
  715. }
  716. return true, nil
  717. }
  718. // 取下载连接地址的文件名,通过URL获取
  719. func Net_GetUrlFileName(url string) (fileName string) {
  720. lin := url
  721. for true {
  722. if strings.Index(lin, "/") != -1 {
  723. comma := strings.Index(lin, "/")
  724. lin = lin[comma+len("/"):]
  725. } else {
  726. break
  727. }
  728. }
  729. if strings.Index(lin, "?") != -1 {
  730. comma := strings.Index(lin, "?")
  731. lin = lin[:comma]
  732. }
  733. fileName = lin
  734. return
  735. }
  736. /*
  737. Net_Url_S5_EX
  738. 使用Socks5访问(可以设置代理/超时时间/Cookie/请求类型/协议头等) . V 1.0.0 2022.7.14
  739. methed [选]
  740. urlstr [必]要访问的URL
  741. data [选]POST数据
  742. proxy [选]代理服务器地址 . 为空则不使用代理 。 账密代理格式 {proxyUrl:proxyPort}@{proxyUser}:{proxyPass} 例如 127.0.0.1:8888@user:password; 仅代理格式 {proxyUrl:proxyPort} 例如 127.0.0.1:8888
  743. cookies [选]传入访问使用的Cookies . 为空则不使用
  744. outtime [选]超时时长,单位 秒 ,默认6秒
  745. headers [选]协议头 如 headers := make(map[string]string)
  746. headers["Content-Type"] = "application/json;charset=utf-8"
  747. headers["token"] = token
  748. headers["Connection"] = "keep-alive"
  749. headers["Accept"] = "*\/*" 去掉\
  750. headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
  751. headers["X-Requested-With"] = "XMLHttpRequest"
  752. headers["Referer"] = "http://www.xxxx.com/"
  753. autoredirect [选]是否自动重定向, 1:自动处理 ; 2.不处理 ; 默认为0:自动处理
  754. 返回
  755. body:接收的数据 , (字符串方式 string(body))
  756. err:错误信息
  757. Recookies 返回的Cookies 。 可以配合 Net_CookieCookToStr 使用
  758. */
  759. func Net_Url_S5_EX(methed, urlstr, data, proxy, cookies string, outtime int, headers map[string]string, autoredirect int) (body []byte, err error, ReCookies string, ReHeader http.Header) {
  760. if methed == "" || len(methed) < 2 {
  761. methed = "GET"
  762. }
  763. if outtime < 1 {
  764. outtime = 6
  765. }
  766. var client *http.Client
  767. if proxy == "" {
  768. client = &http.Client{
  769. Timeout: time.Second * time.Duration(outtime),
  770. }
  771. } else {
  772. urli := url.URL{}
  773. var proxyUrl, proxyUser, proxyPass string
  774. if !strings.Contains(proxy, "socks5") {
  775. proxy = fmt.Sprintf("socks5://%s", proxy)
  776. }
  777. if strings.Index(proxy, "@") == -1 { // 仅代理URL方式
  778. proxyUrl = proxy
  779. } else {
  780. comma := strings.Index(proxy, "@")
  781. proxyUrl = proxy[:comma]
  782. lin := proxy[comma+len("@"):]
  783. if len(lin) > 0 {
  784. if strings.Index(lin, ":") == -1 {
  785. proxyUser = lin
  786. proxyPass = ""
  787. } else {
  788. comma = strings.Index(lin, ":")
  789. proxyUser = lin[:comma]
  790. proxyPass = lin[comma+len(":"):]
  791. }
  792. }
  793. }
  794. urlProxy, _ := urli.Parse(proxyUrl)
  795. if proxyUser != "" && proxyPass != "" {
  796. urlProxy.User = url.UserPassword(proxyUser, proxyPass)
  797. }
  798. client = &http.Client{
  799. Timeout: time.Second * time.Duration(outtime),
  800. Transport: &http.Transport{
  801. Proxy: http.ProxyURL(urlProxy),
  802. },
  803. }
  804. }
  805. var rqt *http.Request
  806. rqt, err = http.NewRequest(methed, urlstr, bytes.NewReader([]byte(data)))
  807. if err != nil {
  808. return
  809. }
  810. if len(headers) == 0 { // 判断是否需要自动补充基本协议头
  811. rqt.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4664.110 Safari/537.36")
  812. } else {
  813. for key, header := range headers {
  814. rqt.Header.Set(key, header)
  815. }
  816. }
  817. if len(cookies) > 1 {
  818. rqt.Header.Add("Cookie", cookies)
  819. }
  820. if autoredirect == 2 { // 处理重定向使用
  821. client.CheckRedirect = func(rqt *http.Request, via []*http.Request) error {
  822. return http.ErrUseLastResponse
  823. }
  824. }
  825. // rqt.Header.Add("User-Agent", "xxx")
  826. //处理返回结果
  827. var response *http.Response
  828. response, err = client.Do(rqt)
  829. if response != nil {
  830. defer func(Body io.ReadCloser) {
  831. err := Body.Close()
  832. if err != nil {
  833. fmt.Println("client.Do Err > " + err.Error())
  834. }
  835. }(response.Body)
  836. }
  837. defer func() {
  838. if e := recover(); e != nil {
  839. fmt.Printf("Panicing %s\r\n", e)
  840. }
  841. }()
  842. if err != nil {
  843. return []byte(""), err, "", nil
  844. }
  845. cooklin := response.Cookies()
  846. for _, v := range cooklin {
  847. ReCookies = ReCookies + v.Raw + ";"
  848. }
  849. ReHeader = response.Header.Clone()
  850. body, err = ioutil.ReadAll(response.Body)
  851. return
  852. }