Skip to content

Commit

Permalink
tproxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Feb 24, 2018
1 parent d13b214 commit 048edc9
Show file tree
Hide file tree
Showing 8 changed files with 807 additions and 0 deletions.
82 changes: 82 additions & 0 deletions OPENSOURCELICENSES
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,56 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

### go-cache

https://github.com/patrickmn/go-cache

Copyright (c) 2012-2017 Patrick Mylund Nielsen and the go-cache contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

### go-tproxy

https://github.com/LiamHaworth/go-tproxy

MIT License

Copyright (c) 2017 Liam R. Haworth

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

### mux

https://github.com/gorilla/mux
Expand Down Expand Up @@ -107,6 +157,38 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

### net

https://github.com/golang/net

Copyright (c) 2009 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

### open-golang

https://github.com/skratchdot/open-golang
Expand Down
46 changes: 46 additions & 0 deletions cli/brook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"log"
"os"
"runtime"
"strings"

"net/http"
Expand Down Expand Up @@ -722,6 +723,51 @@ func main() {
},
},
}
if runtime.GOOS == "linux" {
app.Commands = append(app.Commands, cli.Command{
Name: "tproxy",
Usage: "Run as tproxy mode",
Flags: []cli.Flag{
cli.StringFlag{
Name: "listen, l",
Usage: "Client listen address, like: 127.0.0.1:1080",
},
cli.StringFlag{
Name: "server, s",
Usage: "Server address, like: 1.2.3.4:1080",
},
cli.StringFlag{
Name: "password, p",
Usage: "Server password",
},
cli.IntFlag{
Name: "tcpTimeout",
Value: 60,
Usage: "connection tcp keepalive timeout (s)",
},
cli.IntFlag{
Name: "tcpDeadline",
Value: 0,
Usage: "connection deadline time (s)",
},
cli.IntFlag{
Name: "udpDeadline",
Value: 60,
Usage: "connection deadline time (s)",
},
},
Action: func(c *cli.Context) error {
if c.String("listen") == "" || c.String("server") == "" || c.String("password") == "" {
cli.ShowCommandHelp(c, "tproxy")
return nil
}
if debug {
enableDebug()
}
return brook.RunTproxy(c.String("listen"), c.String("server"), c.String("password"), c.Int("tcpTimeout"), c.Int("tcpDeadline"), c.Int("udpDeadline"))
},
})
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
Expand Down
10 changes: 10 additions & 0 deletions run_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package brook

// RunTproxy used to start a tproxy
func RunTproxy(address, server, password string, tcpTimeout, tcpDeadline, udpDeadline int) error {
c, err := NewTproxy(address, server, password, tcpTimeout, tcpDeadline, udpDeadline)
if err != nil {
return err
}
return c.ListenAndServe()
}
4 changes: 4 additions & 0 deletions scripts/iproute.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

sudo ip rule add fwmark 1 lookup 100
sudo ip route add local 0.0.0.0/0 dev lo table 100
31 changes: 31 additions & 0 deletions scripts/iptables.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

sudo sysctl -w net.ipv4.ip_forward=1
sudo sysctl -w net.ipv6.conf.all.forwarding=1

sudo iptables -t mangle -F
sudo iptables -t mangle -X
sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT

sudo iptables -t mangle -N BROOK

sudo iptables -t mangle -A BROOK -d 0.0.0.0/8 -j RETURN
sudo iptables -t mangle -A BROOK -d 10.0.0.0/8 -j RETURN
sudo iptables -t mangle -A BROOK -d 127.0.0.0/8 -j RETURN
sudo iptables -t mangle -A BROOK -d 169.254.0.0/16 -j RETURN
sudo iptables -t mangle -A BROOK -d 172.16.0.0/12 -j RETURN
sudo iptables -t mangle -A BROOK -d 192.168.0.0/16 -j RETURN
sudo iptables -t mangle -A BROOK -d 224.0.0.0/4 -j RETURN
sudo iptables -t mangle -A BROOK -d 240.0.0.0/4 -j RETURN
sudo iptables -t mangle -A BROOK -d BROOK_SERVER_IP -j RETURN

sudo iptables -t mangle -A BROOK -j MARK --set-mark 1
sudo iptables -t mangle -A BROOK -j ACCEPT

sudo iptables -t mangle -A PREROUTING -p tcp -m socket -j BROOK
sudo iptables -t mangle -A PREROUTING -p tcp -j TPROXY --tproxy-mark 0x1/0x1 --on-port BROOK_TPROXY_PORT

sudo iptables -t mangle -A PREROUTING -p udp -m socket -j BROOK
sudo iptables -t mangle -A PREROUTING -p udp -j TPROXY --tproxy-mark 0x1/0x1 --on-port BROOK_TPROXY_PORT
111 changes: 111 additions & 0 deletions tproxy/tcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package tproxy

import (
"net"
"os"
"strconv"
"strings"
"syscall"

"github.com/txthinking/ant"
)

func ListenTCP(network string, laddr *net.TCPAddr) (*net.TCPListener, error) {
l, err := net.ListenTCP(network, laddr)
if err != nil {
return nil, err
}
defer l.Close()

f, err := l.File()
if err != nil {
return nil, err
}
defer f.Close()
fd := int(f.Fd())
if err := syscall.SetsockoptInt(fd, syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil {
return nil, err
}
tmp, err := net.FileListener(f)
if err != nil {
return nil, err
}
return tmp.(*net.TCPListener), nil
}

func DialTCP(network, addr string) (net.Conn, error) {
taddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return nil, err
}
saddr, err := tcpAddrToSocketAddr(taddr)
if err != nil {
return nil, err
}
fd, err := syscall.Socket(tcpAddrFamily("tcp", taddr, nil), syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
if err != nil {
return nil, err
}
if err := syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
syscall.Close(fd)
return nil, err
}
if err := syscall.SetsockoptInt(fd, syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil {
syscall.Close(fd)
return nil, err
}
if err := syscall.SetNonblock(fd, true); err != nil {
syscall.Close(fd)
return nil, err
}
if err = syscall.Connect(fd, saddr); err != nil && !strings.Contains(err.Error(), "operation now in progress") {
syscall.Close(fd)
return nil, err
}

f := os.NewFile(uintptr(fd), string(ant.RandomNumber()))
defer f.Close()

c, err := net.FileConn(f)
if err != nil {
syscall.Close(fd)
return nil, err
}
return c, nil
}

func tcpAddrToSocketAddr(addr *net.TCPAddr) (syscall.Sockaddr, error) {
switch {
case addr.IP.To4() != nil:
ip := [4]byte{}
copy(ip[:], addr.IP.To4())

return &syscall.SockaddrInet4{Addr: ip, Port: addr.Port}, nil

default:
ip := [16]byte{}
copy(ip[:], addr.IP.To16())

zoneID, err := strconv.ParseUint(addr.Zone, 10, 32)
if err != nil {
return nil, err
}

return &syscall.SockaddrInet6{Addr: ip, Port: addr.Port, ZoneId: uint32(zoneID)}, nil
}
}

func tcpAddrFamily(net string, laddr, raddr *net.TCPAddr) int {
switch net[len(net)-1] {
case '4':
return syscall.AF_INET
case '6':
return syscall.AF_INET6
}

if (laddr == nil || laddr.IP.To4() != nil) &&
(raddr == nil || raddr.IP.To4() != nil) {
return syscall.AF_INET
}
return syscall.AF_INET6
}
Loading

0 comments on commit 048edc9

Please sign in to comment.