Exemple #1
0
func BuildUpstreamRouter(conf Config) *UpstreamRouter {
	var routers []socks.Router
	for _, upstreamConf := range conf.AllUpstreamConfig {
		var router socks.Router
		router = NewDirectRouter(conf.DNSCacheTimeout)
		switch strings.ToLower(upstreamConf.ServerType) {
		case "socks5":
			{
				clientFactory := func(conn net.Conn) SOCKClient {
					return socks.NewSOCKS5Client(conn)
				}

				router = NewSOCKSRouter(upstreamConf.Addr, router, clientFactory,
					CipherConnDecorator(upstreamConf.CryptoMethod, upstreamConf.Password))
			}
		case "shadowsocks":
			{
				clientFactory := func(conn net.Conn) SOCKClient {
					return socks.NewShadowSocksClient(conn)
				}
				router = NewSOCKSRouter(upstreamConf.Addr, router, clientFactory,
					CipherConnDecorator(upstreamConf.CryptoMethod, upstreamConf.Password))
			}
		}
		routers = append(routers, router)
	}
	if len(routers) == 0 {
		router := NewDirectRouter(conf.DNSCacheTimeout)
		routers = append(routers, router)
	}
	return NewUpstreamRouter(NewUpstreamRouterBalancer(routers))
}
Exemple #2
0
func BuildUpstream(upstream Upstream, forward socks.Dialer) (socks.Dialer, error) {
	cipherDecorator := NewCipherConnDecorator(upstream.Crypto, upstream.Password)
	forward = NewDecorateClient(forward, cipherDecorator)

	switch strings.ToLower(upstream.Type) {
	case "socks5":
		{
			return socks.NewSocks5Client("tcp", upstream.Address, "", "", forward)
		}
	case "shadowsocks":
		{
			return socks.NewShadowSocksClient("tcp", upstream.Address, forward)
		}
	}
	return nil, errors.New("unknown upstream type" + upstream.Type)
}