forked from fatedier/frp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refine pkg net utils (fatedier#2720)
* refactor: refine pkg net utils * fix: x Co-authored-by: blizard863 <[email protected]>
- Loading branch information
1 parent
0fb6aee
commit ea568e8
Showing
6 changed files
with
142 additions
and
60 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
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,89 @@ | ||
package net | ||
|
||
import ( | ||
"crypto/tls" | ||
"net" | ||
) | ||
|
||
type dialOptions struct { | ||
proxyURL string | ||
protocol string | ||
tlsConfig *tls.Config | ||
disableCustomTLSHeadByte bool | ||
} | ||
|
||
type DialOption interface { | ||
apply(*dialOptions) | ||
} | ||
|
||
type EmptyDialOption struct{} | ||
|
||
func (EmptyDialOption) apply(*dialOptions) {} | ||
|
||
type funcDialOption struct { | ||
f func(*dialOptions) | ||
} | ||
|
||
func (fdo *funcDialOption) apply(do *dialOptions) { | ||
fdo.f(do) | ||
} | ||
|
||
func newFuncDialOption(f func(*dialOptions)) *funcDialOption { | ||
return &funcDialOption{ | ||
f: f, | ||
} | ||
} | ||
|
||
func DefaultDialOptions() dialOptions { | ||
return dialOptions{ | ||
protocol: "tcp", | ||
} | ||
} | ||
|
||
func WithProxyURL(proxyURL string) DialOption { | ||
return newFuncDialOption(func(do *dialOptions) { | ||
do.proxyURL = proxyURL | ||
}) | ||
} | ||
|
||
func WithTLSConfig(tlsConfig *tls.Config) DialOption { | ||
return newFuncDialOption(func(do *dialOptions) { | ||
do.tlsConfig = tlsConfig | ||
}) | ||
} | ||
|
||
func WithDisableCustomTLSHeadByte(disableCustomTLSHeadByte bool) DialOption { | ||
return newFuncDialOption(func(do *dialOptions) { | ||
do.disableCustomTLSHeadByte = disableCustomTLSHeadByte | ||
}) | ||
} | ||
|
||
func WithProtocol(protocol string) DialOption { | ||
return newFuncDialOption(func(do *dialOptions) { | ||
do.protocol = protocol | ||
}) | ||
} | ||
|
||
func DialWithOptions(addr string, opts ...DialOption) (c net.Conn, err error) { | ||
op := DefaultDialOptions() | ||
|
||
for _, opt := range opts { | ||
opt.apply(&op) | ||
} | ||
|
||
if op.proxyURL == "" { | ||
c, err = ConnectServer(op.protocol, addr) | ||
} else { | ||
c, err = ConnectServerByProxy(op.proxyURL, op.protocol, addr) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if op.tlsConfig == nil { | ||
return | ||
} | ||
|
||
c = WrapTLSClientConn(c, op.tlsConfig, op.disableCustomTLSHeadByte) | ||
return | ||
} |
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