0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 21:30:16 +02:00

allow proxying connection (#283)

This commit is contained in:
Zack 2020-10-05 08:30:45 -07:00 committed by GitHub
parent e75b75164f
commit 3396743d73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 5 deletions

2
go.mod
View file

@ -22,7 +22,7 @@ require (
github.com/stretchr/testify v1.4.0 github.com/stretchr/testify v1.4.0
github.com/tscholl2/siec v0.0.0-20191122224205-8da93652b094 github.com/tscholl2/siec v0.0.0-20191122224205-8da93652b094
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect golang.org/x/net v0.0.0-20200904194848-62affa334b73
golang.org/x/sys v0.0.0-20200916084744-dbad9cb7cb7a // indirect golang.org/x/sys v0.0.0-20200916084744-dbad9cb7cb7a // indirect
golang.org/x/text v0.3.3 // indirect golang.org/x/text v0.3.3 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect

View file

@ -14,6 +14,7 @@ import (
"time" "time"
"github.com/schollz/cli/v2" "github.com/schollz/cli/v2"
"github.com/schollz/croc/v8/src/comm"
"github.com/schollz/croc/v8/src/croc" "github.com/schollz/croc/v8/src/croc"
"github.com/schollz/croc/v8/src/models" "github.com/schollz/croc/v8/src/models"
"github.com/schollz/croc/v8/src/tcp" "github.com/schollz/croc/v8/src/tcp"
@ -84,6 +85,7 @@ func Run() (err error) {
&cli.StringFlag{Name: "relay6", Value: models.DEFAULT_RELAY6, Usage: "ipv6 address of the relay", EnvVars: []string{"CROC_RELAY6"}}, &cli.StringFlag{Name: "relay6", Value: models.DEFAULT_RELAY6, Usage: "ipv6 address of the relay", EnvVars: []string{"CROC_RELAY6"}},
&cli.StringFlag{Name: "out", Value: ".", Usage: "specify an output folder to receive the file"}, &cli.StringFlag{Name: "out", Value: ".", Usage: "specify an output folder to receive the file"},
&cli.StringFlag{Name: "pass", Value: "pass123", Usage: "password for the relay", EnvVars: []string{"CROC_PASS"}}, &cli.StringFlag{Name: "pass", Value: "pass123", Usage: "password for the relay", EnvVars: []string{"CROC_PASS"}},
&cli.StringFlag{Name: "socks5", Value: "", Usage: "add a socks5 proxy", EnvVars: []string{"http_proxy"}},
} }
app.EnableBashCompletion = true app.EnableBashCompletion = true
app.HideHelp = false app.HideHelp = false
@ -92,10 +94,10 @@ func Run() (err error) {
allStringsAreFiles := func(strs []string) bool { allStringsAreFiles := func(strs []string) bool {
for _, str := range strs { for _, str := range strs {
if !utils.Exists(str) { if !utils.Exists(str) {
return false; return false
} }
} }
return true; return true
} }
// if trying to send but forgot send, let the user know // if trying to send but forgot send, let the user know
@ -159,6 +161,7 @@ func determinePass(c *cli.Context) (pass string) {
func send(c *cli.Context) (err error) { func send(c *cli.Context) (err error) {
setDebugLevel(c) setDebugLevel(c)
comm.Socks5Proxy = c.String("socks5")
crocOptions := croc.Options{ crocOptions := croc.Options{
SharedSecret: c.String("code"), SharedSecret: c.String("code"),
IsSender: true, IsSender: true,
@ -356,6 +359,7 @@ func saveConfig(c *cli.Context, crocOptions croc.Options) {
} }
func receive(c *cli.Context) (err error) { func receive(c *cli.Context) (err error) {
comm.Socks5Proxy = c.String("socks5")
crocOptions := croc.Options{ crocOptions := croc.Options{
SharedSecret: c.String("code"), SharedSecret: c.String("code"),
IsSender: false, IsSender: false,

View file

@ -8,9 +8,13 @@ import (
"net" "net"
"time" "time"
"github.com/schollz/croc/v8/src/utils"
log "github.com/schollz/logger" log "github.com/schollz/logger"
"golang.org/x/net/proxy"
) )
var Socks5Proxy = ""
const MAXBYTES = 4000000 const MAXBYTES = 4000000
// Comm is some basic TCP communication // Comm is some basic TCP communication
@ -24,7 +28,18 @@ func NewConnection(address string, timelimit ...time.Duration) (c *Comm, err err
if len(timelimit) > 0 { if len(timelimit) > 0 {
tlimit = timelimit[0] tlimit = timelimit[0]
} }
connection, err := net.DialTimeout("tcp", address, tlimit) var connection net.Conn
if Socks5Proxy != "" && !utils.IsLocalIP(address) {
var dialer proxy.Dialer
dialer, err = proxy.SOCKS5("tcp", Socks5Proxy, nil, proxy.Direct)
if err != nil {
err = fmt.Errorf("proxy failed: %w", err)
return
}
connection, err = dialer.Dial("tcp", address)
} else {
connection, err = net.DialTimeout("tcp", address, tlimit)
}
if err != nil { if err != nil {
err = fmt.Errorf("comm.NewConnection failed: %w", err) err = fmt.Errorf("comm.NewConnection failed: %w", err)
return return

View file

@ -261,3 +261,43 @@ func FindOpenPorts(host string, portNumStart, numPorts int) (openPorts []int) {
} }
return return
} }
// local ip determination
// https://stackoverflow.com/questions/41240761/check-if-ip-address-is-in-private-network-space
var privateIPBlocks []*net.IPNet
func init() {
for _, cidr := range []string{
"127.0.0.0/8", // IPv4 loopback
"10.0.0.0/8", // RFC1918
"172.16.0.0/12", // RFC1918
"192.168.0.0/16", // RFC1918
"169.254.0.0/16", // RFC3927 link-local
"::1/128", // IPv6 loopback
"fe80::/10", // IPv6 link-local
"fc00::/7", // IPv6 unique local addr
} {
_, block, err := net.ParseCIDR(cidr)
if err != nil {
panic(fmt.Errorf("parse error on %q: %v", cidr, err))
}
privateIPBlocks = append(privateIPBlocks, block)
}
}
func IsLocalIP(ipaddress string) bool {
if strings.Contains(ipaddress, "localhost") {
return true
}
host, _, _ := net.SplitHostPort(ipaddress)
ip := net.ParseIP(host)
if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
return true
}
for _, block := range privateIPBlocks {
if block.Contains(ip) {
return true
}
}
return false
}

View file

@ -191,3 +191,7 @@ func TestFindOpenPorts(t *testing.T) {
openPorts := FindOpenPorts("localhost", 9009, 4) openPorts := FindOpenPorts("localhost", 9009, 4)
assert.Equal(t, []int{9009, 9010, 9011, 9012}, openPorts) assert.Equal(t, []int{9009, 9010, 9011, 9012}, openPorts)
} }
func TestIsLocalIP(t *testing.T) {
assert.True(t, IsLocalIP("192.168.0.14:9009"))
}