Skip to content

Commit e57694d

Browse files
committed
Add HTTP and SOCKS5 proxy servers as tool command
1 parent 67118d8 commit e57694d

File tree

22 files changed

+1942
-1
lines changed

22 files changed

+1942
-1
lines changed

Gopkg.lock

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
name = "github.com/cenkalti/backoff"
3434
version = "1.1.0"
3535

36+
[[override]]
37+
name = "github.com/elazarl/goproxy"
38+
branch = "master"
39+
3640
[prune]
3741
go-tests = true
3842
unused-packages = true

cmd/tools/tools.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package tools
2+
3+
import (
4+
"github.com/armon/go-socks5"
5+
"github.com/elazarl/goproxy"
6+
"github.com/elazarl/goproxy/ext/auth"
7+
"github.com/sirupsen/logrus"
8+
"github.com/spf13/cobra"
9+
"net/http"
10+
)
11+
12+
var Tools = &cobra.Command{
13+
Use: "tools",
14+
Short: "Tools",
15+
}
16+
17+
var httpProxy = &cobra.Command{
18+
Use: "http-proxy",
19+
Short: "HTTP proxy server",
20+
RunE: httpProxyServer,
21+
}
22+
23+
var socks5Proxy = &cobra.Command{
24+
Use: "socks5-proxy",
25+
Short: "SOCKS5 proxy server",
26+
RunE: socks5ProxyServer,
27+
}
28+
29+
func init() {
30+
Tools.AddCommand(httpProxy)
31+
Tools.AddCommand(socks5Proxy)
32+
33+
Tools.PersistentFlags().String("username", "", `username for proxy authentication`)
34+
Tools.PersistentFlags().String("password", "", "password for proxy authentication")
35+
36+
httpProxy.Flags().String("addr", ":3128", "proxy listen address")
37+
httpProxy.Flags().Bool("verbose", false, "should every proxy request be logged to stdout")
38+
39+
socks5Proxy.Flags().String("addr", ":1080", "proxy listen address")
40+
}
41+
42+
func httpProxyServer(cmd *cobra.Command, _ []string) error {
43+
username, _ := cmd.Flags().GetString("username")
44+
password, _ := cmd.Flags().GetString("password")
45+
addr, _ := cmd.Flags().GetString("addr")
46+
verbose, _ := cmd.Flags().GetBool("verbose")
47+
48+
proxy := goproxy.NewProxyHttpServer()
49+
proxy.Verbose = verbose
50+
if username != "" && password != "" {
51+
logrus.Info("HTTP proxy will require basic authentication")
52+
53+
proxy.OnRequest().HandleConnect(auth.BasicConnect("", func(user, passwd string) bool {
54+
return user == username && passwd == password
55+
}))
56+
}
57+
58+
logrus.Infof("Starting HTTP proxy server on %s", addr)
59+
return http.ListenAndServe(addr, proxy)
60+
}
61+
62+
func socks5ProxyServer(cmd *cobra.Command, _ []string) error {
63+
username, _ := cmd.Flags().GetString("username")
64+
password, _ := cmd.Flags().GetString("password")
65+
addr, _ := cmd.Flags().GetString("addr")
66+
67+
conf := &socks5.Config{}
68+
server, err := socks5.New(conf)
69+
if err != nil {
70+
return err
71+
}
72+
if username != "" && password != "" {
73+
logrus.Info("SOCKS5 proxy will require basic authentication", addr)
74+
75+
authenticator := &socks5.UserPassAuthenticator{
76+
Credentials: socks5ProxyCredentials{
77+
username: username,
78+
password: password,
79+
},
80+
}
81+
conf.AuthMethods = []socks5.Authenticator{authenticator}
82+
}
83+
84+
logrus.Infof("Starting SOCKS5 proxy server on %s", addr)
85+
return server.ListenAndServe("tcp", addr)
86+
}
87+
88+
type socks5ProxyCredentials struct {
89+
username, password string
90+
}
91+
92+
func (s socks5ProxyCredentials) Valid(username, password string) bool {
93+
return s.username == username && s.password == password
94+
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"github.com/grepplabs/kafka-proxy/cmd/kafka-proxy"
6+
"github.com/grepplabs/kafka-proxy/cmd/tools"
67
"github.com/spf13/cobra"
78
"os"
89
)
@@ -20,6 +21,7 @@ var RootCmd = &cobra.Command{
2021
func init() {
2122
RootCmd.AddCommand(server.Server)
2223
RootCmd.AddCommand(server.Version)
24+
RootCmd.AddCommand(tools.Tools)
2325
}
2426

2527
func main() {

vendor/github.com/elazarl/goproxy/.gitignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/elazarl/goproxy/LICENSE

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/elazarl/goproxy/README.md

Lines changed: 122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/elazarl/goproxy/actions.go

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/elazarl/goproxy/all.bash

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)