Exemple #1
0
func TextChanDialTimeout(t *testing.T) {
	connChan := make(chan ConnRes)
	exitChan := make(chan int)
	defer close(exitChan)

	dial, err := proxyclient.NewProxyClient("direct://0.0.0.0:0000/")
	if err != nil {
		panic(err)
	}

	go func() {
		ChanDialTimeout(dial, 0, connChan, exitChan, true, "user data 111", nil, "tcp", "www.baidu.com:80", 5*time.Second)
		close(connChan)
		close(exitChan)
	}()

	i := 0

	for _ = range connChan {
		i++
	}

	if i == 0 {
		t.Errorf("错误,未成功建立至少1个连接。")
	}

}
Exemple #2
0
// 还未实现全自动测试
func testPre1(t *testing.T) {
	p, err := proxyclient.NewProxyClient("socks5://127.0.0.1:7070")
	if err != nil {
		t.Fatal(err)
	}

	c, err := p.Dial("tcp", "0.0.0.1:80")
	if err != nil {
		t.Fatal(err)
	}

	if _, err := c.Write([]byte("GET / HTTP/1.0\r\nHOST:www.baidu.com\r\n\r\n")); err != nil {
		t.Fatal(err)
	}

	buf := make([]byte, 1024)

	if n, err := c.Read(buf); err != nil {
		t.Fatal(err)
	} else {
		if bytes.Contains(buf[:n], []byte("Content-Type")) == false {
			t.Fatal("失败,数据:", string(buf[:n]))
		}
	}
}
Exemple #3
0
func main() {
	p, err := proxyclient.NewProxyClient("http://127.0.0.1:7777")
	if err != nil {
		panic(err)
	}

	t := time.Now()
	c, err := p.Dial("tcp", "www.google.com:80")
	if err != nil {
		panic(err)
	}
	fmt.Println("连接耗时:", time.Now().Sub(t))

	if _, err := c.Write([]byte("GET / HTTP/1.0\r\nHOST:www.google.com\r\n\r\n")); err != nil {
		panic(err)
	}

	b := make([]byte, 2048)

	if n, err := c.Read(b); err != nil {
		panic(err)
	} else {
		fmt.Print(string(b[:n]))
	}

	c.Close()

}
Exemple #4
0
func main() {
	// 启用 http 协议分包(将 http 协议拆分到多个包)
	// 建立连接后第一次发送数据等待 5000 毫秒
	p, err := proxyclient.NewProxyClient("direct://0.0.0.0:0000?SplitHttp=true&sleep=5000")
	if err != nil {
		panic(err)
	}

	t := time.Now()
	c, err := p.Dial("tcp", "www.baidu.com:80")
	if err != nil {
		panic(err)
	}
	fmt.Println("连接耗时:", time.Now().Sub(t))

	if _, err := c.Write([]byte("GET / HTTP/1.0\r\nHOST:www.baidu.com\r\n\r\n")); err != nil {
		panic(err)
	}

	b := make([]byte, 2048)

	if n, err := c.Read(b); err != nil {
		panic(err)
	} else {
		fmt.Print(string(b[:n]))
	}

	c.Close()

}
Exemple #5
0
func NewBaseUpStream(srv *Server) (*baseUpStream, error) {
	url := "http://127.0.0.1:7777"
	pc, err := proxyclient.NewProxyClient(url)
	if err != nil {
		return nil, fmt.Errorf("无法创建上层代理:%v", err)
	}
	return &baseUpStream{srv, pc, url}, nil
}
Exemple #6
0
func main() {
	p, err := proxyclient.NewProxyClient("ss://*****:*****@www.proxy.net:1234")
	if err != nil {
		panic("创建代理客户端错误")
	}

	c, err := p.Dial("tcp", "www.google.com:80")
	if err != nil {
		panic("连接错误")
	}

	io.WriteString(c, "GET / HTTP/1.0\r\nHOST:www.google.com\r\n\r\n")
	b, err := ioutil.ReadAll(c)
	if err != nil {
		panic("读错误")
	}
	fmt.Print(string(b))
}
Exemple #7
0
func main() {
	p, err := proxyclient.NewProxyClient("socks5://127.0.0.1:5556")
	if err != nil {
		panic("创建代理客户端错误")
	}

	c, err := p.Dial("tcp", "www.google.com:80")
	if err != nil {
		panic("连接错误")
	}

	io.WriteString(c, "GET / HTTP/1.0\r\nHOST:www.google.com\r\n\r\n")
	b, err := ioutil.ReadAll(c)
	if err != nil {
		panic("读错误")
	}
	fmt.Print(string(b))
}
Exemple #8
0
func main() {
	p, err := proxyclient.NewProxyClient("socks5://user1:[email protected]:6789")
	if err != nil {
		panic("创建代理客户端错误")
	}

	c, err := p.Dial("tcp", "www.163.com:80")
	if err != nil {
		panic("连接错误" + err.Error())
	}

	io.WriteString(c, "GET / HTTP/1.0\r\nHOST:www.163.com\r\n\r\n")
	b, err := ioutil.ReadAll(c)
	if err != nil {
		panic("读错误")
	}
	fmt.Print(string(b))
}
Exemple #9
0
func main() {
	connChan := make(chan netchan.ConnRes)
	exitChan := make(chan int)
	defer close(exitChan)

	dial, err := proxyclient.NewProxyClient("direct://0.0.0.0:0000/")
	if err != nil {
		panic(err)
	}

	go func() {
		netchan.ChanDialTimeout(dial, 0, connChan, exitChan, true, nil, nil, "tcp", "www.163.com:80", 5*time.Second)
		close(connChan)
	}()

	for c := range connChan {
		fmt.Printf("目标 %v 耗时 %v 。\r\n", c.Conn.RemoteAddr(), c.Ping)
	}

}
func (su *tcppingUpStream) AddUpStream(name, proxyUrl string, dnsResolve bool, credit int, sleep time.Duration, correctDelay time.Duration) error {
	// 加入线路

	pc, err := proxyclient.NewProxyClient(proxyUrl)
	if err != nil {
		return fmt.Errorf("无法创建上级代理:%v", err)
	}

	dialCredit := 0
	creditQuery, ok := pc.GetProxyAddrQuery()["credit"]
	if ok {
		if len(creditQuery) > 1 {
			return fmt.Errorf("代理 credit 重复设置,代理url:%v", proxyUrl)
		}
		dialCreditTem, err := strconv.Atoi(creditQuery[0])
		if err == nil {
			dialCredit = dialCreditTem
		}
	}

	su.dc = append(su.dc, DialClient{name, dnsResolve, pc, dialCredit, sleep, correctDelay})
	return nil
}
Exemple #11
0
func TestHttpsProxy(t *testing.T) {
	if _, err := os.Stat("tls.crt"); err != nil {
		t.Skip("不存在 crt ,跳过 https 单元测试。")
	}

	httpProxy, err := NewHttpServer("")
	if err != nil {
		t.Fatal(err)
	}

	httpsProxy, err := NewTlsServer(":18043", "proxy.com", "", "tls.crt", "tls.key", httpProxy)
	if err != nil {
		panic(err)
	}

	go httpsProxy.ListenAndServe()

	l, err := net.Listen("tcp", ":15364")
	if err != nil {
		panic(err)
	}

	// 执行连接
	go func() {
		p, err := proxyclient.NewProxyClient("https://127.0.0.1:18043?insecureskipverify=true&domain=proxy.com&standardheader=true")
		if err != nil {
			panic(err)
		}
		t.Log(p.GetProxyAddrQuery())

		c, err := p.Dial("tcp", "127.0.0.1:15364")
		if err != nil {
			panic(err)
		}
		c.SetDeadline(time.Now().Add(3 * time.Second))

		buf := make([]byte, 50)

		if n, err := c.Read(buf); err != nil {
			panic(err)
		} else {
			buf = buf[:n]
		}

		if _, err := c.Write(buf); err != nil {
			panic(err)
		}
	}()

	okChan := make(chan int)

	time.AfterFunc(10*time.Second, func() { okChan <- 0 })

	go func() {
		c, err := l.Accept()
		if err != nil {
			panic(err)
		}
		c.SetDeadline(time.Now().Add(3 * time.Second))

		buf := []byte("xb4wh6awgfdjgffgdttjng3q")

		if _, err := c.Write(buf); err != nil {
			panic(err)
		}

		buf2 := make([]byte, len(buf))

		if _, err := c.Read(buf2); err != nil {
			panic(err)
		}

		if bytes.Compare(buf, buf2) != 0 {
			panic("buf!=buf2")
		}

		okChan <- 1
	}()

	ok := <-okChan

	if ok == 0 {
		t.Fatal("ok==0")
	}
}
Exemple #12
0
func TestHttpProxy(t *testing.T) {
	httpProxy, err := NewHttpServer(":18048")
	if err != nil {
		t.Fatal(err)
	}
	go httpProxy.ListenAndServe()

	l, err := net.Listen("tcp", ":15365")
	if err != nil {
		panic(err)
	}

	// 执行连接
	go func() {
		p, err := proxyclient.NewProxyClient("http://127.0.0.1:18048")
		if err != nil {
			panic(err)
		}
		c, err := p.Dial("tcp", "127.0.0.1:15365")
		if err != nil {
			panic(err)
		}

		c.SetDeadline(time.Now().Add(3 * time.Second))

		buf := make([]byte, 50)

		if n, err := c.Read(buf); err != nil {
			panic(err)
		} else {
			buf = buf[:n]
		}

		if _, err := c.Write(buf); err != nil {
			panic(err)
		}
	}()

	okChan := make(chan int)

	time.AfterFunc(10*time.Second, func() { okChan <- 0 })

	go func() {
		c, err := l.Accept()
		if err != nil {
			panic(err)
		}

		c.SetDeadline(time.Now().Add(3 * time.Second))

		buf := []byte("xb4wh6awgfdjgffgdttjng3q")

		if _, err := c.Write(buf); err != nil {
			panic(err)
		}

		buf2 := make([]byte, len(buf))

		if _, err := c.Read(buf2); err != nil {
			panic(err)
		}

		if bytes.Compare(buf, buf2) != 0 {
			panic("buf!=buf2")
		}

		okChan <- 1
	}()

	ok := <-okChan

	if ok == 0 {
		t.Fatal("ok==0")
	}
}
func (d *DialClients) Config(config *ConfigDialClients) (rerr error) {

	newClients := make([]*DialClient, 0, len(config.UpStreams))

	newUf, err := ufile.NewUFile(config.BasePath, 1*time.Minute)
	if err != nil {
		return err
	}
	// 出错退出时关闭 UFile
	defer func() {
		if rerr != nil {
			newUf.Close()
		}
	}()

	// 循环处理每个线路
	for _, c := range config.UpStreams {

		pc, err := proxyclient.NewProxyClient(c.ProxyUrl)
		if err != nil {
			rerr = fmt.Errorf("无法创建上级代理:%v", err)
			return
		}

		client := DialClient{
			name:         c.Name,
			dnsResolve:   c.DnsResolve,
			pc:           pc,
			dialCredit:   c.Credit,
			sleep:        time.Duration(c.Sleep) * time.Millisecond,
			correctDelay: time.Duration(c.CorrectDelay) * time.Millisecond,
			whiteList:    domains.NewDomains(100),
			blackList:    domains.NewDomains(100),
		}

		// 循环处理每个黑白名单文件
		// 添加到 ufile 里面,实际重新加载在 ufile 结果信道处处理。
		flist := func(clientList []*ConfigDialClientWBList, wbList *domains.Domains) error {
			for _, f := range clientList {
				UpdateInterval, err := time.ParseDuration(f.UpdateInterval)
				if err != nil {
					return err
				}

				domainType, err := domains.ParseDomainType(f.Type)
				if err != nil {
					return err
				}

				userdara := ufUserdata{
					name:       fmt.Sprintf("%v-%v", f.Type, f.Path),
					domainType: domainType,
					domains:    wbList,
				}

				if err := newUf.Add(f.Path, UpdateInterval, &userdara); err != nil {
					return err
				}
			}
			return nil
		}

		if err := flist(c.Whitelist, client.whiteList); err != nil {
			rerr = err
			return
		}
		if err := flist(c.Blacklist, client.blackList); err != nil {
			rerr = err
			return
		}

		newClients = append(newClients, &client)
	}

	d.rwm.Lock()
	defer d.rwm.Unlock()
	d.clients = newClients

	if d.uf != nil {
		d.uf.Close()
	}
	d.uf = newUf

	go d.loop(newUf)

	return nil
}