Skip to content

Commit 8eab677

Browse files
authored
Merge pull request #167 from SenseUnit/set-dst
set-dst:// dialer
2 parents fb4cc9f + cd9e4d9 commit 8eab677

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ Supported proxy schemes are:
431431
* `force-resolve` - pseudo-proxy forcing domain name resolve before it will be passed to the next proxy in the chain. Must wrap actual proxy dialer in the chain (i.e. be specified in last `-proxy` argument). Example: `force-resolve://`
432432
* `chain` - pseudo-proxy assembling a chain from other proxy specifications. Query string parameters are:
433433
* `next` - specification of the next proxy in chain. This query string parameter can be specified more than once. Example: `chain://?next=http%3A%2F%2F127.0.0.1%3A57800&next=force-resolve%3A%2F%2F`.
434+
* `set-dst` - pseudo-proxy replacing connection destination address with the one specified in hostname part of URI. It is mostly intended for programmatic usage from within routing JS script. Example: `set-dst://localhost:9999`.
434435

435436
## Configuration files
436437

dialer/dialer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func init() {
2121
xproxy.RegisterDialerType("h2", H2ProxyDialerFromURL)
2222
xproxy.RegisterDialerType("h2c", H2ProxyDialerFromURL)
2323
xproxy.RegisterDialerType("set-src-hints", NewHintsSettingDialerFromURL)
24+
xproxy.RegisterDialerType("set-dst", NewFixedDstDialerFromURL)
2425
xproxy.RegisterDialerType("cached", GetCachedDialer)
2526
xproxy.RegisterDialerType("socks5s", SOCKS5SDialerFromURL)
2627
xproxy.RegisterDialerType("socks5hs", SOCKS5SDialerFromURL)

dialer/fixed.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package dialer
2+
3+
import (
4+
"context"
5+
"errors"
6+
"net"
7+
"net/url"
8+
9+
xproxy "golang.org/x/net/proxy"
10+
)
11+
12+
type FixedDstDialer struct {
13+
addr string
14+
next Dialer
15+
}
16+
17+
func NewFixedDstDialerFromURL(u *url.URL, next xproxy.Dialer) (xproxy.Dialer, error) {
18+
host, port := u.Hostname(), u.Port()
19+
if host == "" {
20+
return nil, errors.New("missing hostname")
21+
}
22+
if port == "" {
23+
return nil, errors.New("missing port")
24+
}
25+
return &FixedDstDialer{
26+
addr: net.JoinHostPort(host, port),
27+
next: MaybeWrapWithContextDialer(next),
28+
}, nil
29+
}
30+
31+
func (d *FixedDstDialer) Dial(network, address string) (net.Conn, error) {
32+
return d.DialContext(context.Background(), network, address)
33+
}
34+
35+
func (d *FixedDstDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
36+
return d.next.DialContext(ctx, network, d.addr)
37+
}
38+
39+
func (d *FixedDstDialer) WantsHostname(_ context.Context, _, _ string) bool {
40+
// there is no point resolving address which we will discard anyway
41+
return true
42+
}
43+
44+
var _ Dialer = new(FixedDstDialer)
45+
var _ HostnameWanter = new(FixedDstDialer)

0 commit comments

Comments
 (0)