socks5 proxy using go

huangapple go评论151阅读模式
英文:

socks5 proxy using go

问题

我想知道是否可以监听本地端口,例如1080的socks5,并且将该端口上的所有连接作为代理连接到外部主机:端口的socks5。

func main() {
    l, err := net.Listen("tcp", "127.0.0.1:1080")
    if err != nil {
        fmt.Print(err)
    }
    defer l.Close()

    for {
        conn, err := l.Accept()
        if err != nil {
            fmt.Print(err)
        }

        go handle(conn)
    }
}

func handle(conn net.Conn) {
    defer conn.Close()

    dialect, err := proxy.SOCKS5("tcp", "externalhost:externalport", nil, proxy.Direct)

    newConn, err := dialect.Dial("tcp", "targethost:targetport")
    if err != nil {
        log.Printf("Connection error: %s", err.Error())
    }

    go func() {
        _, err = io.Copy(newConn, conn)
        if err != nil {
            log.Printf("Connection error: %s", err.Error())
        }
    }()

    _, err = io.Copy(conn, newConn)
    if err != nil {
        log.Printf("Connection error: %s", err.Error())
    }
}
func handle(conn net.Conn) {
    defer conn.Close()
}

我需要获取目标地址并验证连接是否为socks5,然后使用外部IP进行代理,并将其传递给dialect.Dial函数。

英文:

I wanted to know if it is possible to listen to a local port ex: 1080 socks5, and all connections on that port be made a proxy to use an external host:port socks5

func main() {
	l, err := net.Listen("tcp", "127.0.0.1:1080")
	if err != nil {
		fmt.Print(err)
	}
	defer l.Close()

	for {
		conn, err := l.Accept()
		if err != nil {
			fmt.Print(err)
		}

		go handle(conn)
	}
}

func handle(conn net.Conn) {
	defer conn.Close()

	dialect, err := proxy.SOCKS5("tcp", "externalhost:externalport", nil, proxy.Direct)

	newConn, err := dialect.Dial("tcp", "targethost:targetport")
	if err != nil {
		log.Printf("Connection error: %s", err.Error())
	}

	go func() {
		_, err = io.Copy(newConn, conn)
		if err != nil {
			log.Printf("Connection error: %s", err.Error())
		}
	}()

	_, err = io.Copy(conn, newConn)
	if err != nil {
		log.Printf("Connection error: %s", err.Error())
	}
}
func handle(conn net.Conn) {
	defer conn.Close()
}

I would need to get the destination addres and validate if the connection is socks5 and then perform the proxy with the external ip and pass it in the dialect.dial

答案1

得分: 0

听起来你想要这样的功能:

  1. 一个工具,在特定端口上监听你的本地机器的TCP连接。
  2. 你可以向该端口发送socks5协议的请求,它应该将这些请求全部转发到另一台远程机器上的socks5服务器。
  3. 那个socks5服务器负责与socks5协议请求中的目标建立连接。

在这种情况下,你只需要一个基本的TCP代理工具。你的工具不需要解析socks5请求,也不需要使用proxy.SOCKS5来连接远程机器。你只需要将所有连接转发到远程端点。

你目前的代码基本上可以工作,只是你应该使用net.Dial(而不是dialect.Dial)来连接到"externalhost:externalport",而且你不需要创建proxy.SOCKS5拨号器。

英文:

It sounds like you want this:

  1. A tool that listens on your local machine using TCP at a particular port
  2. You can make socks5-protocol requests to that port, and it should forward these requests all to one other socks5 server on another remote machine.
  3. That socks5 server is responsible for making the connections to the targets in the socks5-protocol requests

In that case, you only need a basic TCP proxy. Your tool doesn't need to look inside the socks5 request, and it doesn't need proxy.SOCKS5 to connect to the remote machine. You just want all connections to your local endpoint to be forwarded to your remote endpoint.

Your current code would largely work, exception you should just use net.Dial (instead of dialect.Dial) to connect to "externalhost:externalport", and you don't need to create the proxy.SOCKS5 dialer.

huangapple
  • 本文由 发表于 2023年1月4日 07:16:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/74999581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定