Skip to content

Commit

Permalink
Implement --preset-ips
Browse files Browse the repository at this point in the history
  • Loading branch information
Jipok committed Oct 28, 2024
1 parent 5343e02 commit 423153a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ Options:
--interface, -i Use existing network interface (OpenVPN, WireGuard, etc.)
--proxy-list Domains to route through specified interface [default: proxy.lst]
--block-list Domains to block [default: blocks.lst]
--preset-ips File with IP addresses to proxy immediately, without waiting for DNS resolution
--silent, -s Don't show when new routes are added
--verbose, -v Enable verbose output
--persistent, -p Keep WireGuard interface (if created) and routes after exit
--help, -h Show this help message
--version Show version
Multiple proxy/block/ips lists can be specified using semicolon (;)
Example: proxy1.lst;proxy2.lst;proxy3.lst
```

## How It Works
Expand Down
18 changes: 9 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ const (
)

type Args struct {
WGConfig string `arg:"positional" help:"Path to WireGuard configuration file"`
Interface string `arg:"-i,--interface" help:"Use existing WireGuard interface instead of creating new one from config"`
ProxyList string `arg:"--proxy-list" default:"proxy.lst" help:"File with list of domains to proxy through WireGuard(or specified interface)"`
BlockList string `arg:"--block-list" default:"blocks.lst" help:"File with list of domains to block completely"`
// IpList string ``
Force bool `arg:"-f,--force" help:"Force remove existing dnsr-wg interface and create new one"`
Silent bool `arg:"-s,--silent" help:"Don't show when new routes are added"`
Verbose bool `arg:"-v,--verbose" help:"Enable verbose output for all DNS-answers"`
Persistent bool `arg:"-p,--persistent" help:"Keep WireGuard interface (if created) and routes after exit"`
WGConfig string `arg:"positional" help:"Path to WireGuard configuration file"`
Interface string `arg:"-i,--interface" help:"Use existing WireGuard interface instead of creating new one from config"`
ProxyList string `arg:"--proxy-list" default:"proxy.lst" help:"File with list of domains to proxy through WireGuard(or specified interface)"`
BlockList string `arg:"--block-list" default:"blocks.lst" help:"File with list of domains to block completely"`
PresetIPs string `arg:"--preset-ips" help:"File with IP addresses to proxy immediately, without waiting for DNS resolution"`
Force bool `arg:"-f,--force" help:"Force remove existing dnsr-wg interface and create new one"`
Silent bool `arg:"-s,--silent" help:"Don't show when new routes are added"`
Verbose bool `arg:"-v,--verbose" help:"Enable verbose output for all DNS-answers"`
Persistent bool `arg:"-p,--persistent" help:"Keep WireGuard interface (if created) and routes after exit"`
}

func (Args) Version() string {
Expand Down
55 changes: 52 additions & 3 deletions routing.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"bufio"
"fmt"
"log"
"net"
"os"
"strings"

"github.com/vishvananda/netlink"
)
Expand Down Expand Up @@ -31,7 +34,52 @@ func setupRouting() {
if len(proxyIPset.set) > 0 {
log.Printf(yellow("WARNING! ")+"found %d collisions in routes table! Will be treated as own.", len(proxyIPset.set))
}
// log.Println(green("Routing setup completed"))

// Load user preset
count := 0
for _, source := range strings.Split(args.PresetIPs, ";") {
source = strings.TrimSpace(source)
if source == "" {
continue
}
file, err := os.Open(source)
if err != nil {
log.Fatalf(red("Error")+" opening file %s: %v", source, err)
continue
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// Remove comment
if idx := strings.Index(line, "#"); idx != -1 {
line = line[:idx]
}
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Parse IP
ip := net.ParseIP(line)
if ip == nil {
log.Printf(yellow("Can't parse line in %s: ")+"%s", source, line)
continue
}
if addRoute(ip) {
count++
} else {
log.Printf(yellow(" %s"), ip.String())
}
}
if err := scanner.Err(); err != nil {
log.Fatalf(red("Error")+" reading file %s: %v", source, err)
}
}

if args.PresetIPs != "" {
log.Printf("Routing %d preset IP addresses", count)
}
}

func cleanupRouting() {
Expand Down Expand Up @@ -75,8 +123,7 @@ func singleHostRoute(ip net.IP) *net.IPNet {
}
}

func addRoute(ip net.IP) {
ip.DefaultMask()
func addRoute(ip net.IP) bool {
newRoute := &netlink.Route{
LinkIndex: link.Attrs().Index,
Scope: netlink.SCOPE_UNIVERSE,
Expand All @@ -86,7 +133,9 @@ func addRoute(ip net.IP) {
err := netlink.RouteAdd(newRoute)
if err != nil {
log.Printf(red("Error:")+" adding route: %v", err)
return false
}
return true
}

func delRoute(ip net.IP) {
Expand Down

0 comments on commit 423153a

Please sign in to comment.