Ejemplo n.º 1
0
func (s TorPTTransport) Parse() (starter server.Starter, err error) {

	handler := server.NewHandler(s.Name)

	remoteAddr := server.NewOption("remoteaddr")
	err = remoteAddr.Required()
	if err != nil {
		handler.PrintError(err.Error())
		return
	}
	s.RemoteAddr = remoteAddr.Get()

	secret := server.NewOption("secret")
	err = secret.Required()
	if err != nil {
		handler.PrintError(err.Error())
		return
	}

	err = s.Args.UnmarshalJSON([]byte(secret.Get()))
	if err != nil {
		handler.PrintError(err.Error())
		return
	}
	handler.PrintVersion()
	return s, nil

}
Ejemplo n.º 2
0
func (m MockTransport) Parse() (starter server.Starter, err error) {
	handler := server.NewHandler(m.Name)
	handler.PrintVersion()
	bindAddr := server.NewOption("bindaddr")
	m.BindAddr = bindAddr.Get()
	return m, nil
}
Ejemplo n.º 3
0
func (m MockTransport) Start() error {
	handler := server.NewHandler(m.Name)
	if failToStart {
		return handler.PrintError("no fun")
	} else {
		handler.PrintExpose("socks5", "127.0.0.1:1")
		handler.PrintDone()
	}
	return nil
}
Ejemplo n.º 4
0
func (s Socks5Transport) Start() error {
	handler := server.NewHandler(s.Name)

	socksConf := &socks5.Config{}
	server, err := socks5.New(socksConf)
	if err != nil {
		_ = handler.PrintError(err.Error())
		return err
	}

	listener, err := net.Listen("tcp", s.BindAddr)
	if err != nil {
		_ = handler.PrintError(err.Error())
		return err
	}
	listener = handler.MonitorListener(listener)
	defer func() {
		err := listener.Close()
		if err != nil {
			handler.Logln(err)
		}

	}()

	handler.PrintExpose("socks5", listener.Addr().String())
	handler.PrintDone()

	serve := func(l net.Listener) error {
		for {
			conn, err := l.Accept()
			if err != nil {
				return err
			}
			go func(conn net.Conn) {
				conn = handler.MonitorConn(conn)
				defer handler.TrackCloseConn(conn.RemoteAddr().String())
				handler.TrackOpenConn(conn.RemoteAddr().String())
				err := server.ServeConn(conn)
				if err != nil {
					handler.Logln(err)
				}

			}(conn)
		}
	}

	go func() {
		err = serve(listener)
		if err != nil {
			handler.Fatal(err)
		}
	}()
	handler.Wait()
	return nil
}
Ejemplo n.º 5
0
func (s Socks5Transport) Parse() (starter server.Starter, err error) {
	handler := server.NewHandler(s.Name)
	err = handler.PrintVersion()
	if err != nil {
		return nil, err
	}
	// handler := server.NewHandler(s.Name)
	bindAddr := server.NewOption("bindaddr")
	s.BindAddr = bindAddr.Get()
	return s, nil
}
Ejemplo n.º 6
0
func (s ShadowsocksTransport) Parse() (starter server.Starter, err error) {

	handler := server.NewHandler(s.Name)
	handler.PrintVersion()

	bindAddr := server.NewOption("bindaddr")
	bindAddr.Default("127.0.0.1:0")

	_, bindPortStr, err := net.SplitHostPort(bindAddr.BindAddr())
	if err != nil {
		handler.PrintError(err.Error())
		return
	}
	s.BindAddr = bindAddr.Get()
	bindPort, err := strconv.Atoi(bindPortStr)
	if err != nil {
		handler.PrintError(err.Error())
		return
	}
	s.BindPort = bindPort

	remoteAddr := server.NewOption("remoteaddr")
	err = remoteAddr.Required()
	if err != nil {
		handler.PrintError(err.Error())
		return
	}
	s.RemoteAddr = remoteAddr.Get()

	remoteHost, remotePortStr, err := net.SplitHostPort(remoteAddr.Get())
	if err != nil {
		handler.PrintError(err.Error())
		return
	}
	s.RemoteHost = remoteHost

	remotePort, err := strconv.Atoi(remotePortStr)
	if err != nil {
		handler.PrintError(err.Error())
		return
	}
	s.RemotePort = remotePort

	secret := server.NewOption("secret")
	err = secret.Required()
	if err != nil {
		handler.PrintError(err.Error())
		return
	}
	s.Secret = secret.Get()
	return s, nil
}
Ejemplo n.º 7
0
func (s ShadowsocksTransport) Start() error {
	handler := server.NewHandler(s.Name)

	listener, err := net.Listen("tcp", s.BindAddr)
	if err != nil {
		listener, err = net.Listen("tcp", "127.0.0.1:0")
	}
	if err != nil {
		handler.PrintError("CANNOT BIND TO ANY PORT")
		return errors.New("Cannot bind to any port")
	}

	listener = handler.MonitorListener(listener)
	handler.PrintExpose("socks5", listener.Addr().String())

	config := &ss.Config{
		Server:     s.RemoteHost,
		ServerPort: s.RemotePort,
		Password:   s.Secret,
		LocalPort:  s.BindPort,
		Method:     "aes-256-cfb",
	}

	parseServerConfig(config)

	handler.PrintDone()

	verbose := server.NewOption("verbose")

	if verbose.Has() {
		s.Verbose = true
		ss.SetDebug(true)
		debug = true
	}

	go func() {
		for {
			defer listener.Close()
			conn, err := listener.Accept()
			if err != nil {
				debug.Println("accept:", err)
				continue
			}
			go func(conn net.Conn) {
				conn = handler.MonitorConn(conn)
				if debug {
					debug.Printf("socks connect from %s\n", conn.RemoteAddr().String())
				}
				closed := false
				defer func() {
					if !closed {
						conn.Close()
					}
				}()

				var err error = nil
				if err = handShake(conn); err != nil {
					debug.Printf("socks handshake: %s", err)
					return
				}
				rawaddr, addr, err := getRequest(conn)
				if err != nil {
					debug.Println("error getting request:", err)
					return
				}
				// Sending connection established message immediately to client.
				// This some round trip time for creating socks connection with the client.
				// But if connection failed, the client will get connection reset error.
				_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})
				if err != nil {
					debug.Println("send connection confirmation:", err)
					return
				}

				remote, err := createServerConn(rawaddr, addr)
				if err != nil {
					if len(servers.srvCipher) > 1 {
						debug.Println("Failed connect to all avaiable shadowsocks server")
					}
					return
				}
				handler.TrackOpenConn(addr)
				defer func() {
					if !closed {
						remote.Close()
					}
				}()

				go ss.PipeThenClose(conn, remote)
				ss.PipeThenClose(remote, conn)
				closed = true
				handler.TrackCloseConn(addr)

			}(conn)
		}
	}()
	handler.Wait()
	return nil
}
Ejemplo n.º 8
0
func (s TorPTTransport) Start() error {
	handler := server.NewHandler(s.Name)

	client := ptc.Client{
		Config: ptc.Config{
			ExitOnStdinClose: true,
			StateLocation:    configdir,
			Command:          os.Args[0],
		},
		Methods: []string{s.Name},
	}

	err := client.Start()
	if err != nil {
		_ = handler.PrintError(err.Error())
		return err
	}
	ptmethod, err := client.Method(s.Name)
	if err != nil {
		handler.PrintError(err.Error())
		return err
	}

	dialconf := ptc.PTDialMethodConfig{
		Args:       s.Args,
		ClientAddr: ptmethod.Addr.String(),
		ServerAddr: s.RemoteAddr,
	}

	dialer, err := dialconf.ClientDialer()
	if err != nil {
		panic(err)
	}

	listener, err := net.Listen("tcp", "127.0.0.1:0")

	if err != nil {
		log.Fatalf("Failed to setup listener: %v", err)
	}

	handler.PrintExpose("socks5", listener.Addr().String())
	handler.PrintDone()

	forward := func(conn net.Conn) {
		conn = handler.MonitorConn(conn)
		client, err := dialer.Dial("tcp", s.RemoteAddr)
		if err != nil {
			log.Println(err)
		}

		client = handler.MonitorConn(client)
		defer client.Close()
		defer conn.Close()
		if err != nil {
			log.Fatalf("Dial failed: %v", err)
		}
		// log.Printf("Connected to localhost %v\n", conn)
		var wg sync.WaitGroup
		wg.Add(2)
		go func() {
			defer wg.Done()
			_, err := io.Copy(client, conn)
			if err != nil {
				log.Println("error", err)
			}

		}()
		go func() {
			defer wg.Done()
			_, err := io.Copy(conn, client)
			if err != nil {
				log.Println("error", err)
			}
		}()
		wg.Wait()
	}

	listener = handler.MonitorListener(listener)

	go func() {
		for {
			conn, err := listener.Accept()
			if err != nil {
				log.Fatalf("ERROR: failed to accept listener: %v", err)
			}
			// log.Printf("Accepted connection %v\n", conn)

			go forward(conn)
		}
	}()
	handler.Wait()
	err = client.Kill()
	if err != nil {
		log.Println(err)
	}
	return nil
}