fedproxy/main.go

65 lines
1.3 KiB
Go
Raw Normal View History

2018-08-29 05:06:07 -07:00
package main
import (
2018-08-29 05:25:37 -07:00
"fmt"
2018-08-29 05:06:07 -07:00
"golang.org/x/net/proxy"
"net"
"os"
2018-08-29 05:25:37 -07:00
"socks5"
2018-08-29 05:06:07 -07:00
"strings"
)
func main() {
args := os.Args[1:]
if len(args) < 2 {
2018-08-29 05:25:27 -07:00
fmt.Printf("usage: %s bindaddr onionsocksaddr [i2psocksaddr]\n", os.Args[0])
2018-08-29 05:06:07 -07:00
return
}
2018-08-29 05:25:27 -07:00
var onion, i2p proxy.Dialer
var err error
onion, err = proxy.SOCKS5("tcp", os.Args[2], nil, nil)
2018-08-29 05:06:07 -07:00
if err != nil {
2018-08-29 05:25:27 -07:00
fmt.Printf("failed to create onion proxy to %s, %s\n", os.Args[2], err.Error())
2018-08-29 05:25:37 -07:00
return
2018-08-29 05:06:07 -07:00
}
2018-09-01 04:26:00 -07:00
if len(args) > 2 {
2018-08-29 05:25:27 -07:00
i2p, err = proxy.SOCKS5("tcp", os.Args[3], nil, nil)
if err != nil {
fmt.Printf("failed to create i2p proxy to %s, %s\n", os.Args[3], err.Error())
return
}
}
2018-08-29 05:06:07 -07:00
serv, err := socks5.New(&socks5.Config{
2018-08-29 05:12:07 -07:00
Dial: func(addr string) (net.Conn, error) {
2018-08-29 05:06:07 -07:00
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
2018-08-29 05:25:27 -07:00
if strings.HasSuffix(host, ".i2p") {
if i2p == nil {
return onion.Dial("tcp", addr)
}
return i2p.Dial("tcp", addr)
}
2018-08-29 05:06:07 -07:00
if strings.HasSuffix(host, ".onion") {
2018-08-29 05:25:27 -07:00
return onion.Dial("tcp", addr)
2018-08-29 05:06:07 -07:00
}
2018-08-29 05:12:07 -07:00
return net.Dial("tcp", addr)
2018-08-29 05:06:07 -07:00
},
})
if err != nil {
2018-08-29 05:25:37 -07:00
fmt.Printf("failed to create socks proxy %s\n", err.Error())
return
2018-08-29 05:06:07 -07:00
}
2018-08-29 05:25:37 -07:00
2018-08-29 05:06:07 -07:00
l, err := net.Listen("tcp", os.Args[1])
if err != nil {
2018-08-29 05:25:27 -07:00
fmt.Printf("failed to listen on %s, %s\n", os.Args[1], err.Error())
2018-08-29 05:06:07 -07:00
return
}
2018-08-29 05:25:27 -07:00
fmt.Printf("proxy serving on %s\n", os.Args[1])
2018-08-29 05:06:07 -07:00
serv.Serve(l)
}