Update to match latest go version.
This commit is contained in:
parent
a55aed58cb
commit
ffb93ccd7e
53
main.go
53
main.go
|
@ -1,14 +1,24 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go.megvii-inc.com/transparent-proxy/server"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"git.eve.moe/jackyyf/transparent-proxy/server"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var port = flag.Int("port", 1080, "Listen port of the server")
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
proxy_listener, err := server.NewIPv4TransparentListener(":9091")
|
flag.Parse()
|
||||||
|
go IPv4Handler()
|
||||||
|
go IPv6Handler()
|
||||||
|
select {}
|
||||||
|
}
|
||||||
|
|
||||||
|
func IPv4Handler() {
|
||||||
|
proxy_listener, err := server.NewIPv4TransparentListener(fmt.Sprintf("0.0.0.0:%d", *port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -18,8 +28,24 @@ func main() {
|
||||||
fmt.Println("Accept Error: ", err.Error())
|
fmt.Println("Accept Error: ", err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Printf("Accepted connection: %s => %s\n", conn.TCPConn().RemoteAddr().String(), conn.RealAddr().String())
|
// fmt.Printf("Accepted connection: %s => %s\n", conn.TCPConn().RemoteAddr().String(), conn.RealAddr().String())
|
||||||
go handle(conn)
|
go handle4(conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func IPv6Handler() {
|
||||||
|
proxy_listener, err := server.NewIPv6TransparentListener(fmt.Sprintf("[::]:%d", *port))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
conn, err := proxy_listener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Accept Error: ", err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// fmt.Printf("Accepted connection: %s => %s\n", conn.TCPConn().RemoteAddr().String(), conn.RealAddr().String())
|
||||||
|
go handle6(conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +62,7 @@ func pipeThenClose(reader io.Reader, writer io.WriteCloser) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
fmt.Println("Reader reached EOF, closing.")
|
// fmt.Println("Reader reached EOF, closing.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -47,13 +73,26 @@ func pipeThenClose(reader io.Reader, writer io.WriteCloser) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handle(conn server.TransparentConnection) {
|
func handle4(conn server.TransparentConnection) {
|
||||||
rconn, err := net.DialTCP("tcp4", nil, conn.RealAddr())
|
rconn, err := net.DialTCP("tcp4", nil, conn.RealAddr())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Connect to ", conn.RealAddr().String(), " error: ", err.Error())
|
fmt.Println("Connect to ", conn.RealAddr().String(), " error: ", err.Error())
|
||||||
conn.TCPConn().Close()
|
conn.TCPConn().Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// fmt.Printf("ESTABLISHED %s <=> %s\n", conn.TCPConn().RemoteAddr().String(), conn.RealAddr().String())
|
||||||
|
go pipeThenClose(conn.TCPConn(), rconn)
|
||||||
|
go pipeThenClose(rconn, conn.TCPConn())
|
||||||
|
}
|
||||||
|
|
||||||
|
func handle6(conn server.TransparentConnection) {
|
||||||
|
rconn, err := net.DialTCP("tcp6", conn.TCPConn().RemoteAddr().(*net.TCPAddr), conn.RealAddr())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Connect to ", conn.RealAddr().String(), " error: ", err.Error())
|
||||||
|
conn.TCPConn().Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// fmt.Printf("ESTABLISHED %s <=> %s\n", conn.TCPConn().RemoteAddr().String(), conn.RealAddr().String())
|
||||||
go pipeThenClose(conn.TCPConn(), rconn)
|
go pipeThenClose(conn.TCPConn(), rconn)
|
||||||
go pipeThenClose(rconn, conn.TCPConn())
|
go pipeThenClose(rconn, conn.TCPConn())
|
||||||
}
|
}
|
|
@ -9,9 +9,9 @@ package server
|
||||||
// WARZONE BEGINS HERE! MIND YOUR HEAD!
|
// WARZONE BEGINS HERE! MIND YOUR HEAD!
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
"net"
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
"net"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -19,13 +19,14 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetFDFromTCPConn(conn *net.TCPConn) int {
|
func GetFDFromTCPConn(conn *net.TCPConn) int {
|
||||||
// Actual fd is stored at: (*(*TCPConn).conn.fd).sysfd
|
// Actual fd is stored at: (*(*TCPConn).conn.fd).pfd.Sysfd
|
||||||
v := reflect.ValueOf(*conn)
|
v := reflect.ValueOf(*conn)
|
||||||
c := v.FieldByName("conn")
|
c := v.FieldByName("conn")
|
||||||
fdp := c.FieldByName("fd")
|
fdp := c.FieldByName("fd")
|
||||||
fd := reflect.Indirect(fdp)
|
fd := reflect.Indirect(fdp)
|
||||||
sysfd := fd.FieldByName("sysfd")
|
pfd := fd.FieldByName("pfd")
|
||||||
return int(sysfd.Int())
|
Sysfd := pfd.FieldByName("Sysfd")
|
||||||
|
return int(Sysfd.Int())
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAddr4FromFD(fd int) *net.TCPAddr {
|
func GetAddr4FromFD(fd int) *net.TCPAddr {
|
||||||
|
@ -43,7 +44,7 @@ func GetAddr4FromFD(fd int) *net.TCPAddr {
|
||||||
Scope_id 4byte ignore for ipv4
|
Scope_id 4byte ignore for ipv4
|
||||||
*/
|
*/
|
||||||
addr.Flowinfo = be32toh(addr.Flowinfo)
|
addr.Flowinfo = be32toh(addr.Flowinfo)
|
||||||
a, b, c, d := byte(addr.Flowinfo >> 24), byte((addr.Flowinfo >> 16) & 255), byte((addr.Flowinfo >> 8) & 255), byte(addr.Flowinfo & 255)
|
a, b, c, d := byte(addr.Flowinfo>>24), byte((addr.Flowinfo>>16)&255), byte((addr.Flowinfo>>8)&255), byte(addr.Flowinfo&255)
|
||||||
ip := net.IPv4(a, b, c, d)
|
ip := net.IPv4(a, b, c, d)
|
||||||
return &net.TCPAddr{IP: ip, Port: int(be16toh(addr.Port)), Zone: ""}
|
return &net.TCPAddr{IP: ip, Port: int(be16toh(addr.Port)), Zone: ""}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue