-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (115 loc) · 3.3 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
package main
import (
"flag"
"fmt"
"io"
"log"
"net"
"os"
"strings"
"time"
prxy "golang.org/x/net/proxy"
)
const (
logFileTpl = "/tmp/stdproxy_%d.log"
authEnvVar = "PROXY_CREDS"
version = "0.1.0"
)
func forwardStd(conn net.Conn) {
c := make(chan int64)
// Read from Reader and write to Writer until EOF
copy := func(r io.ReadCloser, w io.WriteCloser) {
defer func() {
r.Close()
w.Close()
}()
n, _ := io.Copy(w, r)
c <- n
}
go copy(conn, os.Stdout)
go copy(os.Stdin, conn)
b := <-c
log.Printf("[%s]: Connection has been closed by remote peer, %d bytes has been received\n", conn.RemoteAddr(), b)
b = <-c
log.Printf("[%s]: Local peer has been stopped, %d bytes has been sent\n", conn.RemoteAddr(), b)
}
func getProxyAuth(credsfilename string) *prxy.Auth {
creds, _ := os.LookupEnv(authEnvVar)
if credsfilename != "" {
content, err := os.ReadFile(credsfilename)
if err != nil {
log.Fatalf("Error reading proxy credentials file '%s': %s\n", credsfilename, err)
}
creds = string(content)
}
if creds = strings.TrimSpace(creds); creds == "" {
log.Fatalf("Proxy credentials not found")
}
parts := strings.Split(creds, ":")
if len(parts) != 2 {
log.Fatalf("Credential format is incorrect must be '<user>:<password>'")
}
return &prxy.Auth{
User: parts[0],
Password: parts[1],
}
}
func proxyPass(proxy string, destination string, timeout time.Duration, auth *prxy.Auth) {
dailer, _ := prxy.SOCKS5("tcp", proxy, auth, &net.Dialer{
Timeout: timeout,
KeepAlive: 30 * time.Second,
})
conn, err := dailer.Dial("tcp", destination)
if err != nil {
log.Fatalf("Error opening proxy connection: %s\n", err)
}
log.Println("Proxy connection opened")
forwardStd(conn)
}
func main() {
var (
proxyTimeout = flag.Duration("timeout", 5*time.Second, "Proxy connection timeout")
basicAuth = flag.Bool("basic-auth", false, "Use basic authentication (default is to read 'PROXY_CREDS' environment variable)")
basicAuthCredsFile = flag.String("creds-file", "", "Filepath of proxy credentials")
logEnable = flag.Bool("log", false, "Enable logging")
logfile = flag.String("log-file", fmt.Sprintf(logFileTpl, time.Now().Unix()), "Save log execution to file")
versionCmd = flag.Bool("version", false, "Show program version")
)
flag.Parse()
if *versionCmd {
fmt.Printf("v%s\n", version)
os.Exit(0)
}
args := flag.Args()
if len(args) != 3 {
fmt.Println("Program must receive 3 arguments: 'proxyHost:proxyPort destHost destPort'")
os.Exit(1)
}
log.SetFlags(0)
logWriter := io.Discard
if *logEnable {
file, err := os.OpenFile(*logfile, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatalf("Error creating log file: %s", err)
}
logWriter = file
}
log.SetOutput(logWriter)
if *basicAuthCredsFile != "" {
*basicAuth = true
}
proxy := args[0]
destination := fmt.Sprintf("%s:%s", args[1], args[2])
log.Printf("Proxy: %s", proxy)
log.Printf("Proxy Timeout %d:", *proxyTimeout)
log.Printf("Destination: %s", destination)
log.Printf("Basic authentication: %t", *basicAuth)
if *basicAuthCredsFile != "" {
log.Printf("BasicAuth credentials file: '%s'\n", *basicAuthCredsFile)
}
var auth *prxy.Auth
if *basicAuth {
auth = getProxyAuth(*basicAuthCredsFile)
}
proxyPass(proxy, destination, *proxyTimeout, auth)
}