Example #1
0
func startSocksServer(addr string) {
	// start socks server
	socksServer, err := socks.New(addr)
	ce(err, "new socks5 server")
	info("Socks5 server %s started.", addr)
	inBytes := 0
	outBytes := 0
	go func() {
		for _ = range time.NewTicker(time.Second * 8).C {
			info("socks in %s out %s", formatBytes(inBytes), formatBytes(outBytes))
		}
	}()

	// handle socks client
	socksServer.OnSignal("client", func(args ...interface{}) {
		go func() {
			socksClientConn := args[0].(net.Conn)
			defer socksClientConn.Close()
			hostPort := args[1].(string)
			// dial to target addr
			conn, err := net.DialTimeout("tcp", hostPort, time.Second*32)
			if err != nil {
				info("dial error %v", err)
				return
			}
			defer conn.Close()
			// read from target
			go func() {
				buf := make([]byte, 2048)
				var err error
				var n int
				for {
					n, err = conn.Read(buf)
					inBytes += n
					socksClientConn.Write(buf[:n])
					if err != nil {
						return
					}
				}
			}()
			// read from socks client
			buf := make([]byte, 2048)
			var n int
			for {
				n, err = socksClientConn.Read(buf)
				outBytes += n
				conn.Write(buf[:n])
				if err != nil {
					return
				}
			}
		}()
	})
}
Example #2
0
func startLocal(remoteAddr, socksAddr string) {
	// connect to remote
	client, err := van.NewClient(remoteAddr)
	if err != nil {
		log.Fatalf("van.NewClient %v", err)
	}
	defer client.Close()
	p("Connected.\n")

	// set conns
	for i := 0; i < 8; i++ {
		client.NewTransport()
	}
	client.OnSignal("RemoveTransport", func() {
		client.NewTransport()
	})

	// start socks server
	socksServer, err := socks.New(socksAddr)
	if err != nil {
		log.Fatalf("socks5.NewServer %v", err)
	}
	defer socksServer.Close()
	p("Socks5 server started.\n")

	// globals
	socksClientConns := make(map[uint32]net.Conn)
	hostPorts := make(map[uint32]string)

	// handle socks client
	socksServer.OnSignal("client", func(args ...interface{}) {
		go func() {
			conn := client.NewConn()
			socksClientConn := args[0].(net.Conn)
			socksClientConns[conn.Id] = socksClientConn
			hostPort := args[1].(string)
			hostPorts[conn.Id] = string(hostPort)
			// send hostport
			client.Send(conn, obfuscate([]byte(hostPort)))
			// read from socks conn and send to remote
			for {
				data := make([]byte, 2048)
				n, err := socksClientConn.Read(data)
				if err != nil { // socks conn closed
					// send a zero-lengthed packet
					client.Finish(conn)
					return
				}
				data = data[:n]
				client.Send(conn, obfuscate(data))
			}
		}()
	})

	// handle packet from remote
	for {
		packet := <-client.Recv
		switch packet.Type {
		case van.DATA:
			data := obfuscate(packet.Data)
			socksClientConns[packet.Conn.Id].Write(data)
		case van.FIN:
			socksClientConns[packet.Conn.Id].Close()
		}
	}
}