Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add IP address filtering #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,37 @@ import (
)

type Forwarder struct {
src Address
dst Address
dialTimeout int
src Address
dst Address
dialTimeout int
acceptIPFilter string
}

func NewForwarder(src Address, dst Address, dialTimeout int) *Forwarder {
func NewForwarder(src Address, dst Address, dialTimeout int, acceptIPFilter string) *Forwarder {
return &Forwarder{
src: src,
dst: dst,
dialTimeout: dialTimeout,
src: src,
dst: dst,
dialTimeout: dialTimeout,
acceptIPFilter: acceptIPFilter,
}
}

func (f *Forwarder) Start() {
listener, err := net.Listen("tcp", f.src.String())
panicIfErr(err)
_, acceptSubnet, _ := net.ParseCIDR(f.acceptIPFilter)
for {
srcConn, err := listener.Accept()
if err != nil {
println(err.Error())
continue
}
srcIP, _ := net.ResolveTCPAddr("tcp", srcConn.RemoteAddr().String())
if !acceptSubnet.Contains(srcIP.IP) {
println(fmt.Sprintf(`%s -- %s restricted`, time.Now().Format(time.RFC3339), srcIP))
_ = srcConn.Close()
continue
}
go func(srcConn net.Conn) {
dstConn, err := net.DialTimeout("tcp", f.dst.String(), time.Duration(f.dialTimeout)*time.Second)
if err != nil {
Expand Down
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"net"
"os"
"strconv"
"strings"
Expand All @@ -23,6 +24,7 @@ func main() {
remoteHost := flag.String("rHost", "8.8.8.8", "remote host")
remotePort := flag.Int("rPort", 53, "remote port ( will be equal to lPort when doing range forward )")
dialTimeout := flag.Int("timeout", 4, "dial timeout in seconds")
acceptIPFilter := flag.String("acceptIPFilter", "0.0.0.0/0", "subnet of IP addresses from which to accept connections only ( 172.0.0.0/8 for example )")
help := flag.Bool("help", false, "print help")
h := flag.Bool("h", false, "")

Expand All @@ -38,6 +40,10 @@ find me on telegram: @unsafepointer
if *dialTimeout <= 0 || *dialTimeout > 32 {
panic("invalid dial timeout, it should be bigger than 0 and smaller than 32")
}
if _, _, err := net.ParseCIDR(*acceptIPFilter); err != nil {
panic("invalid acceptIPFilter format")
}

src := Address{}
dst := Address{
Host: *remoteHost,
Expand Down Expand Up @@ -68,7 +74,7 @@ find me on telegram: @unsafepointer
}, Address{
Host: *remoteHost,
Port: port,
}, *dialTimeout)
}, *dialTimeout, *acceptIPFilter)
}
} else {
n, err := strconv.Atoi(*listenPort)
Expand All @@ -79,12 +85,12 @@ find me on telegram: @unsafepointer
src.Host = *listenHost
src.Port = n
println(fmt.Sprintf(`[I] tcp://%s <-> tcp://%s`+"\n", src.String(), dst.String()))
go startForwarder(src, dst, *dialTimeout)
go startForwarder(src, dst, *dialTimeout, *acceptIPFilter)
}
select {}
}

func startForwarder(src, dst Address, dialTimeout int) {
forwarder := NewForwarder(src, dst, dialTimeout)
func startForwarder(src, dst Address, dialTimeout int, acceptIPFilter string) {
forwarder := NewForwarder(src, dst, dialTimeout, acceptIPFilter)
forwarder.Start()
}
Binary file modified tcpforwarder
Binary file not shown.
Loading