This repository has been archived by the owner on May 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
136 lines (117 loc) · 3.35 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"github.com/elazarl/goproxy"
"github.com/pkg/errors"
)
var settingsFile string
var lineRegex = regexp.MustCompile(`^([0-9.*]+:[0-9*]+)[\s\t]+([0-9.]+:[0-9]+)$`)
func main() {
var port int
var verbose bool
var help bool
flag.IntVar(&port, "port", 8868, "Port to listen on")
flag.StringVar(&settingsFile, "settings", "settings.txt", "Settings file with routes")
flag.BoolVar(&verbose, "verbose", true, "Verbose proxy output")
flag.BoolVar(&help, "help", false, "Help screen")
flag.Parse()
if help {
helpScreen()
return
}
var routes, err = readSettings()
if err != nil {
return
}
if len(routes) < 1 {
printError(errors.New("no routes defined in settings"))
return
}
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = verbose
for src, dst := range routes {
setupRedirect(proxy, src, dst)
}
fmt.Println()
fmt.Printf("HTTP/S proxy up on port %d!", port)
fmt.Println()
fmt.Println()
log.Fatal(http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", port), proxy))
}
func setupRedirect(proxy *goproxy.ProxyHttpServer, src string, dst string) {
var pattern = getHostMatchRegex(src)
proxy.OnRequest(goproxy.UrlMatches(pattern)).HandleConnectFunc(
func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
return goproxy.OkConnect, dst
})
fmt.Printf("Redirecting %s -> %s", src, dst)
fmt.Println()
}
func getHostMatchRegex(src string) *regexp.Regexp {
var srcSplit = strings.Split(src, ":")
if srcSplit[0] == "*" && srcSplit[1] == "*" {
return regexp.MustCompile(".+")
} else if srcSplit[0] == "*" {
return regexp.MustCompile(fmt.Sprintf("^.+:%s$", srcSplit[1]))
} else if srcSplit[1] == "*" {
return regexp.MustCompile(fmt.Sprintf("^%s:.+$", srcSplit[0]))
} else {
return regexp.MustCompile(fmt.Sprintf("^%s:%s$", srcSplit[0], srcSplit[1]))
}
}
func readSettings() (map[string]string, error) {
if _, err := os.Stat(settingsFile); os.IsNotExist(err) {
err := errors.New("settings file doesn't exist")
printError(err)
return nil, err
}
bytes, err := ioutil.ReadFile(settingsFile)
if err != nil {
return nil, err
}
parsed, err := parseSettings(string(bytes))
if err != nil {
return nil, err
}
return parsed, nil
}
func parseSettings(content string) (map[string]string, error) {
var result = make(map[string]string)
for _, line := range splitLines(content) {
if strings.TrimSpace(line) == "" {
continue
}
matches := lineRegex.FindStringSubmatch(line)
if len(matches) < 2 {
err := errors.New("bad settings format")
printError(err)
return nil, err
}
result[matches[1]] = matches[2]
}
return result, nil
}
func helpScreen() {
fmt.Println(`A HTTP/S proxy that redirects connections.
Designed to be used as system proxy or forced for specific programs via software like Proxifier.`)
fmt.Println()
flag.PrintDefaults()
fmt.Println()
fmt.Println(`Settings file consists of lines defining redirection routes.
A redirection route has the following format:
{ip}:{port} {ip}:{port}
Multiple whitespaces/tabs are permitted as a separator.
This program will redirect the first (source) ip&port to the second (destination) ip&port.
You can use a wildcard '*' to match the ip, port, or both, for the source.
Sample settings.txt:
1.2.3.4:80 127.0.0.1:8080
*:1555 127.0.0.1:6000
*:* 127.0.0.1:80`)
}