-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add socks5 proxy support for kafka output plugin
- Loading branch information
1 parent
8433a46
commit 5981590
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package proxy | ||
|
||
import ( | ||
"golang.org/x/net/proxy" | ||
) | ||
|
||
type Socks5ProxyConfig struct { | ||
Socks5ProxyEnable *bool `toml:"socks5_enable"` | ||
Socks5ProxyAddress string `toml:"socks5_address"` | ||
Socks5ProxyUsername string `toml:"socks5_username"` | ||
Socks5ProxyPassword string `toml:"socks5_password"` | ||
} | ||
|
||
func (c *Socks5ProxyConfig) GetDialer() (proxy.Dialer, error) { | ||
var auth *proxy.Auth | ||
if c.Socks5ProxyPassword != "" || c.Socks5ProxyUsername != "" { | ||
auth = new(proxy.Auth) | ||
auth.User = c.Socks5ProxyUsername | ||
auth.Password = c.Socks5ProxyPassword | ||
} | ||
return proxy.SOCKS5("tcp", c.Socks5ProxyAddress, auth, proxy.Direct) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package proxy | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/armon/go-socks5" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSocks5ProxyConfig(t *testing.T) { | ||
const ( | ||
proxyAddress = "0.0.0.0:12345" | ||
proxyUsername = "user" | ||
proxyPassword = "password" | ||
) | ||
|
||
l, err := net.Listen("tcp", "0.0.0.0:0") | ||
require.NoError(t, err) | ||
|
||
server, err := socks5.New(&socks5.Config{ | ||
AuthMethods: []socks5.Authenticator{socks5.UserPassAuthenticator{ | ||
Credentials: socks5.StaticCredentials{ | ||
proxyUsername: proxyPassword, | ||
}, | ||
}}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
go func() { require.NoError(t, server.ListenAndServe("tcp", proxyAddress)) }() | ||
|
||
enabled := true | ||
conf := Socks5ProxyConfig{ | ||
Socks5ProxyEnable: &enabled, | ||
Socks5ProxyAddress: proxyAddress, | ||
Socks5ProxyUsername: proxyUsername, | ||
Socks5ProxyPassword: proxyPassword, | ||
} | ||
dialer, err := conf.GetDialer() | ||
require.NoError(t, err) | ||
|
||
var proxyConn net.Conn | ||
for i := 0; i < 10; i++ { | ||
proxyConn, err = dialer.Dial("tcp", l.Addr().String()) | ||
if err != nil { | ||
time.Sleep(10 * time.Millisecond) | ||
continue | ||
} | ||
break | ||
} | ||
require.NotNil(t, proxyConn) | ||
defer func() { require.NoError(t, proxyConn.Close()) }() | ||
|
||
serverConn, err := l.Accept() | ||
require.NoError(t, err) | ||
defer func() { require.NoError(t, serverConn.Close()) }() | ||
|
||
writePayload := []byte("test") | ||
_, err = proxyConn.Write(writePayload) | ||
require.NoError(t, err) | ||
|
||
receivePayload := make([]byte, 4) | ||
_, err = serverConn.Read(receivePayload) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, writePayload, receivePayload) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters