代码拉取完成,页面将自动刷新
package bridge_test
import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
_ "github.com/wzshiming/bridge/protocols/command"
_ "github.com/wzshiming/bridge/protocols/connect"
_ "github.com/wzshiming/bridge/protocols/netcat"
_ "github.com/wzshiming/bridge/protocols/shadowsocks"
_ "github.com/wzshiming/bridge/protocols/socks4"
_ "github.com/wzshiming/bridge/protocols/socks5"
_ "github.com/wzshiming/bridge/protocols/ssh"
_ "github.com/wzshiming/bridge/protocols/tls"
_ "github.com/wzshiming/anyproxy/proxies/httpproxy"
_ "github.com/wzshiming/anyproxy/proxies/shadowsocks"
_ "github.com/wzshiming/anyproxy/proxies/socks4"
_ "github.com/wzshiming/anyproxy/proxies/socks5"
_ "github.com/wzshiming/anyproxy/proxies/sshproxy"
"github.com/wzshiming/anyproxy"
"github.com/wzshiming/bridge/chain"
"github.com/wzshiming/bridge/logger"
)
var ctx = context.Background()
func bridge(ctx context.Context, listens, dials []string) error {
b := chain.NewBridge(logger.Std, false)
return b.Bridge(ctx, listens, dials)
}
func MustProxy(addr string) (uri string) {
uri, err := newProxy(addr)
if err != nil {
panic(err)
}
return uri
}
func newProxy(addr string) (uri string, err error) {
u, err := url.Parse(addr)
if err != nil {
return "", err
}
proxy, err := anyproxy.NewAnyProxy(ctx, []string{addr}, &anyproxy.Config{
Dialer: &net.Dialer{},
ListenConfig: &net.ListenConfig{},
})
if err != nil {
return "", err
}
host := proxy.Match(u.Host)
listener, err := net.Listen("tcp", u.Host)
if err != nil {
return "", err
}
u.Host = listener.Addr().String()
go func() {
for {
conn, err := listener.Accept()
if err != nil {
logger.Std.Error(err, "accept")
return
}
go host.ServeConn(conn)
}
}()
return u.String(), nil
}
var ProxyServer = []string{
"socks5://127.0.0.1:0",
"socks4://127.0.0.1:0",
"http://127.0.0.1:0",
"ssh://127.0.0.1:0",
"http://h:p@127.0.0.1:0",
"socks4://s4@127.0.0.1:0",
"socks5://s5:p@127.0.0.1:0",
"ssh://s:p@127.0.0.1:0",
}
func init() {
for i, proxy := range ProxyServer {
ProxyServer[i] = MustProxy(proxy)
logger.Std.Info(ProxyServer[i])
}
}
func TestPortForward(t *testing.T) {
want := "OK"
ser := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte(want))
}))
u, err := url.Parse(ser.URL)
if err != nil {
t.Fatal(err)
}
proxy := getRandomAddress()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
err := bridge(ctx, []string{proxy}, append([]string{u.Host}, ProxyServer...))
if err != nil {
t.Log(err)
}
}()
cli := http.Client{}
for i := 0; i != 10; i++ {
resp, e := cli.Get("http://" + proxy)
if e != nil {
err = e
time.Sleep(time.Second)
continue
}
data, e := io.ReadAll(resp.Body)
if err != nil {
err = e
time.Sleep(time.Second)
continue
}
resp.Body.Close()
if string(data) != want {
err = fmt.Errorf("want %q, got %q", want, data)
time.Sleep(time.Second)
continue
}
err = nil
break
}
if err != nil {
t.Fatal(err)
}
}
func TestPortForwardWithRemoteListen(t *testing.T) {
want := "OK"
ser := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte(want))
}))
u, err := url.Parse(ser.URL)
if err != nil {
t.Fatal(err)
}
proxy := getRandomAddress()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
err := bridge(ctx, append([]string{proxy}, ProxyServer...), []string{u.Host})
if err != nil {
t.Log(err)
}
}()
cli := http.Client{}
for i := 0; i != 10; i++ {
resp, e := cli.Get("http://" + proxy)
if e != nil {
err = e
time.Sleep(time.Second)
continue
}
data, e := io.ReadAll(resp.Body)
if err != nil {
err = e
time.Sleep(time.Second)
continue
}
resp.Body.Close()
if string(data) != want {
err = fmt.Errorf("want %q, got %q", want, data)
time.Sleep(time.Second)
continue
}
err = nil
break
}
if err != nil {
t.Fatal(err)
}
}
func TestProxy(t *testing.T) {
want := "OK"
ser := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte(want))
}))
u, err := url.Parse(ser.URL)
if err != nil {
t.Fatal(err)
}
proxy := getRandomAddress()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
err := bridge(ctx, []string{proxy}, append([]string{"-"}, ProxyServer...))
if err != nil {
t.Log(err)
}
}()
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.Proxy = func(request *http.Request) (*url.URL, error) {
return url.Parse("http://" + proxy)
}
cli := http.Client{
Transport: transport,
}
for i := 0; i != 10; i++ {
resp, e := cli.Get("http://" + u.Host)
if e != nil {
err = e
time.Sleep(time.Second)
continue
}
data, e := io.ReadAll(resp.Body)
if err != nil {
err = e
time.Sleep(time.Second)
continue
}
resp.Body.Close()
if string(data) != want {
err = fmt.Errorf("want %q, got %q", want, data)
time.Sleep(time.Second)
continue
}
err = nil
break
}
if err != nil {
t.Fatal(err)
}
}
func TestProxyWithRemoteListen(t *testing.T) {
want := "OK"
ser := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte(want))
}))
u, err := url.Parse(ser.URL)
if err != nil {
t.Fatal(err)
}
proxy := getRandomAddress()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
err := bridge(ctx, append([]string{proxy}, ProxyServer...), []string{"-"})
if err != nil {
t.Log(err)
}
}()
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.Proxy = func(request *http.Request) (*url.URL, error) {
return url.Parse("http://" + proxy)
}
cli := http.Client{
Transport: transport,
}
for i := 0; i != 10; i++ {
resp, e := cli.Get("http://" + u.Host)
if e != nil {
err = e
time.Sleep(time.Second)
continue
}
data, e := io.ReadAll(resp.Body)
if err != nil {
err = e
time.Sleep(time.Second)
continue
}
resp.Body.Close()
if string(data) != want {
err = fmt.Errorf("want %q, got %q", want, data)
time.Sleep(time.Second)
continue
}
err = nil
break
}
if err != nil {
t.Fatal(err)
}
}
func getRandomAddress() string {
addr, err := net.Listen("tcp", ":0")
if err != nil {
return ""
}
defer addr.Close()
return addr.Addr().String()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。