แสดงบทความที่มีป้ายกำกับ gohls แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ gohls แสดงบทความทั้งหมด

วันอังคารที่ 2 มิถุนายน พ.ศ. 2558

gohls SOCKS client

วันนี้จะโหลดหนังกับ HLS ผ่าน tsocks บน linux แต่ tsocks ดันทำงานไม่ได้ บน ubuntu 15.04 ไม่รู้เป็นเพราะอะไร สั่ง tsocks command แล้วมันไม่ยอมออก socks เลยแก้ปัญหาด้วยการเขียนให้ตัว โหลด HLS มันใช้ socks ได้ด้วยตัวมันเองซะเลย

ตัว HLS download ใช้ตัวเดิมคือ https://github.com/kz26/gohls

ตัว SOCKS lib ใช้ https://github.com/hailiang/socks

แก้ code main.go ของ gohls ตามนี้



package main

import "flag"
import "fmt"
import "io"
import "net/http"
import "net/url"
import "log"
import "os"
import "time"
import "github.com/golang/groupcache/lru"
import "strings"
import "github.com/kz26/m3u8"
import "h12.me/socks"

const VERSION = "1.0.5"

var USER_AGENT string

var dialSocksProxy = socks.DialSocksProxy(socks.SOCKS5, "127.0.0.1:1080")
var tr = &http.Transport{Dial: dialSocksProxy}
var client = &http.Client{Transport: tr}

func doRequest(c *http.Client, req *http.Request) (*http.Response, error) {
        req.Header.Set("User-Agent", USER_AGENT)
        resp, err := c.Do(req)
        return resp, err
}
แค่นี้เราก็สามารถใช้ socks ในโปรแกรมเราได้แล้ว
ส่วนสำคัญคือแก้ code เพิ่ม 4 บรรทัดนี้

import "h12.me/socks"
var dialSocksProxy = socks.DialSocksProxy(socks.SOCKS5, "127.0.0.1:1080")
var tr = &http.Transport{Dial: dialSocksProxy}
var client = &http.Client{Transport: tr}