Skip to content

Commit

Permalink
Merge pull request bepass-org#9 from DanielcoderX/master
Browse files Browse the repository at this point in the history
Enhance CLI Handling and Code Structure in relay.go and udp.go
  • Loading branch information
DanielcoderX authored Sep 5, 2023
2 parents 509752d + 8055fd0 commit 436f533
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 194 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
build/
.idea
.idea
nohup.out
relay
284 changes: 142 additions & 142 deletions relay.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Relay is a package that provides functionality for relaying network traffic.
package main

import (
Expand All @@ -7,154 +8,151 @@ import (
"io"
"log"
"net"
"slices"
"strings"
)

var torrentTrackers = []string{
"93.158.213.92",
"102.223.180.235",
"23.134.88.6",
"185.243.218.213",
"208.83.20.20",
"91.216.110.52",
"83.146.97.90",
"23.157.120.14",
"185.102.219.163",
"163.172.29.130",
"156.234.201.18",
"209.141.59.16",
"34.94.213.23",
"192.3.165.191",
"130.61.55.93",
"109.201.134.183",
"95.31.11.224",
"83.102.180.21",
"192.95.46.115",
"198.100.149.66",
"95.216.74.39",
"51.68.174.87",
"37.187.111.136",
"51.15.79.209",
"45.92.156.182",
"49.12.76.8",
"5.196.89.204",
"62.233.57.13",
"45.9.60.30",
"35.227.12.84",
"179.43.155.30",
"94.243.222.100",
"207.241.231.226",
"207.241.226.111",
"51.159.54.68",
"82.65.115.10",
"95.217.167.10",
"86.57.161.157",
"83.31.30.230",
"94.103.87.87",
"160.119.252.41",
"193.42.111.57",
"80.240.22.46",
"107.189.31.134",
"104.244.79.114",
"85.239.33.28",
"61.222.178.254",
"38.7.201.142",
"51.81.222.188",
"103.196.36.31",
"23.153.248.2",
"73.170.204.100",
"176.31.250.174",
"149.56.179.233",
"212.237.53.230",
"185.68.21.244",
"82.156.24.219",
"216.201.9.155",
"51.15.41.46",
"85.206.172.159",
"104.244.77.87",
"37.27.4.53",
"192.3.165.198",
"15.204.205.14",
"103.122.21.50",
"104.131.98.232",
"173.249.201.201",
"23.254.228.89",
"5.102.159.190",
"65.130.205.148",
"119.28.71.45",
"159.69.65.157",
"160.251.78.190",
"107.189.7.143",
"159.65.224.91",
"185.217.199.21",
"91.224.92.110",
"161.97.67.210",
"51.15.3.74",
"209.126.11.233",
"37.187.95.112",
"167.99.185.219",
"144.91.88.22",
"88.99.2.212",
"37.59.48.81",
"95.179.130.187",
"51.15.26.25",
"192.9.228.30",
}
var (
// List of torrent trackers
torrentTrackers = map[string]bool{
"93.158.213.92": true,
"102.223.180.235": true,
"23.134.88.6": true,
"185.243.218.213": true,
"208.83.20.20": true,
"91.216.110.52": true,
"83.146.97.90": true,
"23.157.120.14": true,
"185.102.219.163": true,
"163.172.29.130": true,
"156.234.201.18": true,
"209.141.59.16": true,
"34.94.213.23": true,
"192.3.165.191": true,
"130.61.55.93": true,
"109.201.134.183": true,
"95.31.11.224": true,
"83.102.180.21": true,
"192.95.46.115": true,
"198.100.149.66": true,
"95.216.74.39": true,
"51.68.174.87": true,
"37.187.111.136": true,
"51.15.79.209": true,
"45.92.156.182": true,
"49.12.76.8": true,
"5.196.89.204": true,
"62.233.57.13": true,
"45.9.60.30": true,
"35.227.12.84": true,
"179.43.155.30": true,
"94.243.222.100": true,
"207.241.231.226": true,
"207.241.226.111": true,
"51.159.54.68": true,
"82.65.115.10": true,
"95.217.167.10": true,
"86.57.161.157": true,
"83.31.30.230": true,
"94.103.87.87": true,
"160.119.252.41": true,
"193.42.111.57": true,
"80.240.22.46": true,
"107.189.31.134": true,
"104.244.79.114": true,
"85.239.33.28": true,
"61.222.178.254": true,
"38.7.201.142": true,
"51.81.222.188": true,
"103.196.36.31": true,
"23.153.248.2": true,
"73.170.204.100": true,
"176.31.250.174": true,
"149.56.179.233": true,
"212.237.53.230": true,
"185.68.21.244": true,
"82.156.24.219": true,
"216.201.9.155": true,
"51.15.41.46": true,
"85.206.172.159": true,
"104.244.77.87": true,
"37.27.4.53": true,
"192.3.165.198": true,
"15.204.205.14": true,
"103.122.21.50": true,
"104.131.98.232": true,
"173.249.201.201": true,
"23.254.228.89": true,
"5.102.159.190": true,
"65.130.205.148": true,
"119.28.71.45": true,
"159.69.65.157": true,
"160.251.78.190": true,
"107.189.7.143": true,
"159.65.224.91": true,
"185.217.199.21": true,
"91.224.92.110": true,
"161.97.67.210": true,
"51.15.3.74": true,
"209.126.11.233": true,
"37.187.95.112": true,
"167.99.185.219": true,
"144.91.88.22": true,
"88.99.2.212": true,
"37.59.48.81": true,
"95.179.130.187": true,
"51.15.26.25": true,
"192.9.228.30": true,
}

func checkIfDestinationIsBlocked(ipAddress string) bool {
if slices.Contains(torrentTrackers, ipAddress) {
return true
// List of Cloudflare IP ranges
cfRanges = []string{
"127.0.0.0/8",
"103.21.244.0/22",
"103.22.200.0/22",
"103.31.4.0/22",
"104.16.0.0/12",
"108.162.192.0/18",
"131.0.72.0/22",
"141.101.64.0/18",
"162.158.0.0/15",
"172.64.0.0/13",
"173.245.48.0/20",
"188.114.96.0/20",
"190.93.240.0/20",
"197.234.240.0/22",
"198.41.128.0/17",
"::1/128",
"2400:cb00::/32",
"2405:8100::/32",
"2405:b500::/32",
"2606:4700::/32",
"2803:f800::/32",
"2c0f:f248::/32",
"2a06:98c0::/29",
}
return false
}

var cfRanges = []string{
"127.0.0.0/8",
"103.21.244.0/22",
"103.22.200.0/22",
"103.31.4.0/22",
"104.16.0.0/12",
"108.162.192.0/18",
"131.0.72.0/22",
"141.101.64.0/18",
"162.158.0.0/15",
"172.64.0.0/13",
"173.245.48.0/20",
"188.114.96.0/20",
"190.93.240.0/20",
"197.234.240.0/22",
"198.41.128.0/17",
"::1/128",
"2400:cb00::/32",
"2405:8100::/32",
"2405:b500::/32",
"2606:4700::/32",
"2803:f800::/32",
"2c0f:f248::/32",
"2a06:98c0::/29",
}
// IP ranges
ipRange []*net.IPNet
)

var ipRange []*net.IPNet
func checkIfDestinationIsBlocked(ipAddress string) bool {
return torrentTrackers[ipAddress]
}

// Server ...
type Server struct {
host string
port string
}

// Client ...
type Client struct {
conn net.Conn
}

// Config ...
type Config struct {
Host string
Port string
}

// New ...
func New(config *Config) *Server {
return &Server{
host: config.Host,
Expand All @@ -163,9 +161,12 @@ func New(config *Config) *Server {
}

func checkIfSourceIsAllowed(ipAddress string) bool {
for _, r := range ipRange {
ip := net.ParseIP(ipAddress)
ip := net.ParseIP(ipAddress)
if ip == nil {
return false
}

for _, r := range ipRange {
if r.Contains(ip) {
return true
}
Expand All @@ -186,7 +187,6 @@ func init() {
}
}

// Run ...
func (server *Server) Run() {
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%s", server.host, server.port))
if err != nil {
Expand All @@ -205,12 +205,12 @@ func (server *Server) Run() {
ip := conn.RemoteAddr().String()
sh, sp, err := net.SplitHostPort(ip)
if err != nil {
fmt.Println(fmt.Errorf("unable to parse host %s", ip))
log.Printf("unable to parse host %s\n", ip)
_ = conn.Close()
continue
}
if !checkIfSourceIsAllowed(sh) {
fmt.Println(fmt.Errorf("request from unacceptable source blocked: %s:%s", sh, sp))
log.Printf("request from unacceptable source blocked: %s:%s\n", sh, sp)
_ = conn.Close()
continue
}
Expand All @@ -227,7 +227,7 @@ func (client *Client) handleRequest() {
_ = client.conn.Close()
}()
reader := bufio.NewReader(client.conn)
header, err := reader.ReadBytes(byte(13))
header, _ := reader.ReadBytes(byte(13))
if len(header) < 1 {
return
}
Expand Down Expand Up @@ -267,7 +267,7 @@ func (client *Client) handleRequest() {
}

if blockFlag {
log.Println(fmt.Errorf("destination host is blocked: %s", address))
log.Printf("destination host is blocked: %s\n", address)
return
}

Expand All @@ -277,7 +277,7 @@ func (client *Client) handleRequest() {
}

// transmit data
log.Printf("%s Dialing to %s...\r\n", network, address)
log.Printf("%s Dialing to %s...\n", network, address)

rConn, err := net.Dial(network, address)

Expand All @@ -293,6 +293,10 @@ func (client *Client) handleRequest() {
_ = rConn.Close()
}

// Copy reads from src and writes to dst until either EOF is reached on src or
// an error occurs. It returns the number of bytes copied and any error
// encountered. Copy uses a fixed-size buffer to efficiently copy data between
// the source and destination.
func Copy(src io.Reader, dst io.Writer) {
buf := make([]byte, 256*1024)

Expand All @@ -303,14 +307,10 @@ func Copy(src io.Reader, dst io.Writer) {
}

func main() {
var ip string
var port string
flag.StringVar(&ip, "b", "0.0.0.0", "Server IP address")
flag.StringVar(&port, "p", "6666", "Server Port number")
var config Config
flag.StringVar(&config.Host, "b", "0.0.0.0", "Server IP address")
flag.StringVar(&config.Port, "p", "6666", "Server Port number")
flag.Parse()
server := New(&Config{
Host: ip,
Port: port,
})
server := New(&config)
server.Run()
}
Loading

0 comments on commit 436f533

Please sign in to comment.