The shared core for proxy and VPN style tools: streams, addresses, dialers and proxy chains, a mixed inbound, routing rules with an Iran preset, share links and v2ray style configs.
Types
A target to connect to: a host (name or IP) and a port, with the kind of host and a validity flag. ParseAddr and NewAddr fill it in.
Name Type Description hoststr = ""The host name or IP text, without brackets for IPv6. portint = 0The port, 0 to 65535. kindint = 3ADDR_IPV4, ADDR_DOMAIN or ADDR_IPV6. The numbers are the ones the SOCKS5 address type uses. validbool = falseFalse when parsing failed. Always check it before using the address. sizeint = 0Filled in by the decoders: how many bytes the encoded address took on the wire.
net-tunnel-Addr.salam Copy
import net.tunnel
func main :
a := tunnel. ParseAddr ( "example.com:443" )
println a.valid, a.kind == tunnel.ADDR_DOMAIN, a.host, a.port, a. String ()
b := tunnel. ParseAddr ( "[2001:db8::5]:8080" )
println b.valid, b.kind == tunnel.ADDR_IPV6, b. String ()
println tunnel. ParseAddr ( "example.com" ).valid, tunnel. ParseAddr ( "a:99999" ).valid
println tunnel. IsIP ( "10.1.1.1" ), tunnel. IsIP ( "example.com" )
ip := tunnel. ParseIPv4 ( "192.168.1.20" )
println ip. Len (), tunnel. FormatIP (ip)
println tunnel. FormatIP (tunnel. ParseIPv6 ( "2001:0db8:0000:0000:0000:0000:0000:0001" ))
println tunnel. FormatIP (tunnel. ParseIP ( "::ffff:1.2.3.4" ))
n := tunnel. NewAddr ( "1.2.3.4" , 80 )
println n.kind == tunnel.ADDR_IPV4, n. String ()
end Output true true example.com 443 example.com:443
true true [2001:db8::5]:8080
false false
true false
4 192.168.1.20
2001:db8::1
::ffff:102:304
true 1.2.3.4:80
Returns the address as host:port, with brackets around an IPv6 host.
The example for this item is shown with the ones above: net-tunnel-Addr.salam
A dialer that chooses another dialer by rules: it asks the router for a tag and dials with the dialer registered under that tag. Use it as the dialer of a ForwardHandler.
Name Type Description routerRouterThe router that picks tags. tagsVector<str>The registered tags, in order. dialersVector<dyn Dialer>The dialers, one for each tag. last_tagstr = ""The tag chosen for the most recent dial, handy in logs and tests.
net-tunnel-RouterDialer.salam Copy
import net.tcp
import net.tunnel
import sync
mut port := 0
mut ready := sync. NewWaitGroup ()
func echo_once ():
srv := tcp. Bind ( 0 )
port = tcp. Port (srv)
sync. Done (ready)
c := tcp. Accept (srv)
tcp. Write (c, "echo:" + tcp. Read (c, 64 ))
tcp. Close (c)
end
func main :
sync. Add (ready, 1 as i64 )
t := spawn (echo_once)
sync. Wait (ready)
mut router := tunnel. NewRouter ( "direct" )
mut blocked := tunnel. NewRule ( "block" )
tunnel. RuleSuffix (blocked, "blocked.test" )
tunnel. RouterAdd (router, blocked)
mut rd := tunnel. NewRouterDialer (router)
tunnel. RouterDialerAdd (rd, "direct" , tunnel. Direct ())
tunnel. RouterDialerAdd (rd, "block" , tunnel. Block ())
println rd. Name (), rd.tags. Len (), rd.dialers. Len ()
s := rd. Dial (tunnel. NewAddr ( "127.0.0.1" , port))
println rd.last_tag, s. Ok ()
s. Write ( "hi" )
println s. Read ( 100 )
s. Close ()
join (t)
println rd. Dial (tunnel. NewAddr ( "ads.blocked.test" , 80 )). Ok (), rd.last_tag
println rd. Pick ( "direct" ). Name (), rd. Pick ( "nothing" ). Name ()
end Output router 2 2
direct true
echo:hi
false block
direct block
func Pick (tag: str ): dyn Dialer Returns the dialer registered under a tag. An unknown tag gives a blocking dialer.
The example for this item is shown with the ones above: net-tunnel-RouterDialer.salam
func Dial (target: Addr): dyn Stream Asks the router for the target's tag, picks that dialer and dials with it.
The example for this item is shown with the ones above: net-tunnel-RouterDialer.salam
The example for this item is shown with the ones above: net-tunnel-RouterDialer.salam
The limits of one server.
Name Type Description activei64*Shared count of admitted connections. max_connectionsint = 1024The cap on connections; 0 for none. max_per_sourceint = 32Handshakes in progress allowed per source; 0 for none. deadline_msint = 10000Total handshake time in milliseconds; 0 for none. sourcesSourceTablePer source counts.
The admission of one connection.
Name Type Description okbool = falseTrue when the connection was admitted. hoststr = ""The source address. ticketi64 = 0The deadline ticket. countedbool = falseTrue when counted against the per source limit. heldbool = falseTrue while a slot is held. donebool = falseTrue once the handshake phase ended.
A connection that has been accepted but not yet opened. Opening does the TLS or WebSocket upgrade on a worker thread, so a slow client cannot stall the accept loop.
net-tunnel-Inbound.salam Copy
import net.tcp
import net.tunnel
import sync
mut echo_port := 0
mut ready := sync. NewWaitGroup ()
struct LineProtocol :
pub unused: int = 0
pub func Name (): str :
ret "line"
end
pub func Handshake (s: dyn tunnel.Stream): tunnel.Session:
line := tunnel. ReadUntil (s, "\n" , 200 )
if not line. StartsWith ( "CONNECT " ):
ret tunnel.Session { error = "bad request" }
end
target := tunnel. ParseAddr (line. Substr ( 8 , line. Len () - 9 ))
if not target.valid:
ret tunnel.Session { error = "bad address" }
end
ret tunnel.Session { ok = true , target = target, protocol = "line" }
end
pub func Reply (s: dyn tunnel.Stream, _sess: tunnel.Session, ok: bool ):
if ok:
s. Write ( "OK\n" )
else :
s. Write ( "ERR\n" )
end
end
end
func echo_server ():
srv := tcp. Bind ( 0 )
echo_port = tcp. Port (srv)
sync. Done (ready)
until true :
c := tcp. Accept (srv)
tcp. Write (c, "echo:" + tcp. Read (c, 100 ))
tcp. Close (c)
end
end
func ask (port: int , request: str ): str :
c := tcp. Dial ( "127.0.0.1" , port)
tcp. Write (c, request)
answer := tcp. Read (c, 100 )
rest := tcp. Read (c, 100 )
tcp. Close (c)
ret answer + rest
end
func main :
sync. Add (ready, 1 as i64 )
_t := spawn (echo_server)
sync. Wait (ready)
proto := LineProtocol {}
forward := tunnel. NewForwarder (tunnel. Direct (), proto)
inbound := tunnel. ListenTcp ( 0 )
println inbound. Ok ()
tunnel. ServeBackground (inbound, proto, forward)
c := tcp. Dial ( "127.0.0.1" , inbound.port)
tcp. Write (c, "CONNECT 127.0.0.1:" + echo_port + "\n" )
println tcp. Read (c, 100 )
tcp. Write (c, "hello" )
println tcp. Read (c, 100 )
tcp. Close (c)
println ask (inbound.port, "CONNECT 127.0.0.1:" + tcp. FreePort () + "\n" )
println ask (inbound.port, "GET / HTTP/1.1\r\n\r\n" ). Len ()
inbound. Stop ()
println forward.stats. Total (), forward.stats. Failed ()
end Output true
OK
echo:hello
ERR
0
2 1
Counts the handshakes in progress per source address, to cap what one address can hold.
Name Type Description corevoid*The shared state.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
Something that accepts connections. TcpAcceptor is the plain one; net.vpn has one for TLS, WebSocket and HTTP upgrade.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
A pending connection that needs no upgrade.
Name Type Description streamdyn StreamThe stream. okbool = falseTrue when the accept worked. remotestr = ""The address of the client.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
True when the accept worked.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
The address of the client.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
Accepts plain TCP connections.
Name Type Description servertcp.ServerThe listening socket.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func Accept (): dyn Pending Waits for the next TCP connection.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
False when listening failed.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
An accept loop with a worker pool. It runs any Acceptor, hands each connection to an InboundProtocol and then to a Handler.
Name Type Description acceptordyn AcceptorThe acceptor. hoststr = "127.0.0.1"The address it listens on. runningatomic.FlagCleared by Stop. workerspool.PoolThe pool that serves connections. handshake_msint = 8000How long a client has to finish the handshake. The default is 8000. per_sourceint = 0How many connections of one address may be in their handshake at once. 0 means no limit. sourcesSourceTableThe handshakes in progress per address.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
False when listening failed.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
Stops accepting and closes the listener.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
The addresses a resolver found for a host name.
Name Type Description ipsVector<str>IPv4 and IPv6 address texts.
net-tunnel-Router.salam Copy
import net.tunnel
func main :
rt := tunnel. IranRouter ( "direct" , "proxy" )
each host in [
"divar.ir" , "WWW.Digikala.com" , "ایران.ir" , "notaparat.com" , "evilir" , "google.com" ,
"192.168.1.9" , "localhost"
]:
println host, rt. MatchHost (host, 443 )
end
mut custom := tunnel. NewRouter ( "proxy" )
mut mail := tunnel. NewRule ( "block" )
tunnel. RulePort (mail, "25,465,587" )
tunnel. RouterAdd (custom, mail)
mut ads := tunnel. NewRule ( "block" )
tunnel. RuleKeyword (ads, "tracker" )
tunnel. RuleRegex (ads, "^ads[0-9]+\\.example\\.com$" )
tunnel. RouterAdd (custom, ads)
mut home := tunnel. NewRule ( "direct" )
tunnel. RuleSuffix (home, "example.org" )
tunnel. RuleDomain (home, "printer.lan" )
tunnel. RuleCidr (home, "203.0.113.0/24" )
tunnel. RouterAdd (custom, home)
mut lan := tunnel. NewRule ( "direct" )
tunnel. RulePrivate (lan)
tunnel. RouterAdd (custom, lan)
println custom. MatchHost ( "smtp.example.com" , 25 )
println custom. MatchHost ( "my-tracker.net" , 443 ), custom. MatchHost ( "ads42.example.com" , 443 )
println custom. MatchHost ( "a.b.example.org" , 443 ), custom. MatchHost ( "printer.lan" , 80 )
println custom. MatchHost ( "203.0.113.77" , 80 ),
custom. MatchHost ( "10.0.0.5" , 22 ),
custom. MatchHost ( "8.8.8.8" , 53 )
println custom. Match (tunnel. ParseAddr ( "203.0.113.1:8080" ))
println tunnel. ParseCidr ( "10.0.0.0/8" ).prefix, tunnel. ParseCidr ( "nonsense" ).kind
mut r := tunnel. NewRule ( "x" )
println tunnel. RuleDomainSpec (r, "full:a.example.com" ),
tunnel. RuleIpSpec (r, "geoip:private" ),
tunnel. RuleCidr (r, "1.2.3.4/40" )
println tunnel. RuleMatches (r, tunnel. ParseAddr ( "a.example.com:80" )),
tunnel. RuleMatches (r, tunnel. ParseAddr ( "b.example.com:80" ))
end Output divar.ir direct
WWW.Digikala.com direct
ایران.ir direct
notaparat.com proxy
evilir proxy
google.com proxy
192.168.1.9 direct
localhost direct
block
block block
direct direct
direct direct proxy
direct
8 0
true true false
true false
Something that turns a host name into addresses. net.vpn has one for net.doh; any lookup function can be wrapped.
The example for this item is shown with the ones above: net-tunnel-Router.salam
A resolver that finds nothing.
Name Type Description unusedint = 0Not used.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func Lookup (_host: str ): LookupResult Always returns no addresses.
The example for this item is shown with the ones above: net-tunnel-Router.salam
The answers a router remembers, so a name is looked up once per time limit.
Name Type Description musync.MutexLock for the cache. ipsHashMap<str, Vector<str>>Addresses by host name. expiresHashMap<str, i64>When each answer stops being valid, in milliseconds. ttl_msi64 = 300000How long an answer is kept. The default is 300000. maxint = 4096How many names to keep before the cache is emptied. lookupsint = 0How many real lookups were made.
The example for this item is shown with the ones above: net-tunnel-Router.salam
One condition of a rule. Fill it with the Rule... functions rather than by hand.
Name Type Description kindint = 0One of the M_ constants. valuestr = ""The text of the condition. netVector<int>For a CIDR: the network address as numbers. prefixint = 0For a CIDR: the prefix length in bits. loint = 0For a port range: the lowest port. hiint = 0For a port range: the highest port. dsetDomainSetFor a geosite matcher: the domain set. isetIpSetFor a geoip matcher: the address set.
The example for this item is shown with the ones above: net-tunnel-Router.salam
A tag plus destination and port conditions. A target matches when it satisfies a destination matcher (if any) and a port matcher (if any).
Name Type Description tagstr = ""The outbound tag the rule picks. destVector<Matcher>Destination matchers: any one is enough. portsVector<Matcher>Port matchers: any one is enough.
The example for this item is shown with the ones above: net-tunnel-Router.salam
An ordered list of rules and a default tag. The first rule that matches decides. A rule with no conditions never matches.
Name Type Description rulesVector<Rule>The rules in the order they are tried. default_tagstr = "direct"The tag returned when no rule matches. resolverdyn HostResolverUsed to turn a domain target into addresses so that CIDR and geoip rules can match it. has_resolverbool = falseTrue after SetResolver. cacheResolveCacheThe remembered answers of the resolver.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func Resolve (host: str ): Vector < Vector < int >> Looks a name up through the resolver and the cache. Returns the addresses as numbers.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func Match (target: Addr): str Returns the tag for a target address.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func MatchHost (host: str , port: int ): str Returns the tag for a host text and a port. Case and a trailing dot of the host are ignored.
The example for this item is shown with the ones above: net-tunnel-Router.salam
What ParseRules returns: the router and the list of lines it could not use.
Name Type Description routerRouterThe router built from the good lines. errorsVector<str>One message per bad line, with its line number.
net-tunnel-Rules.salam Copy
import net.tunnel
func main :
text := "# send Iranian sites direct\nDOMAIN-SUFFIX,ir,direct\nDOMAIN-KEYWORD,tracker,block\nIP-CIDR,203.0.113.0/24,direct\nGEOIP,PRIVATE,direct\nDST-PORT,25,block\nthis line is broken\nFINAL,proxy\n"
set := tunnel. ParseRules (text, "proxy" )
println "errors:" , set.errors. Len ()
each e in set.errors:
println e
end
each host in [ "shop.ir" , "my-tracker.net" , "203.0.113.5" , "127.0.0.1" , "example.com" ]:
println host, set.router. MatchHost (host, 443 )
end
println set.router. MatchHost ( "example.com" , 25 )
missing := tunnel. LoadRules ( "/nonexistent/rules.txt" , "direct" )
println missing.errors. Get ( 0 )
end Output errors: 1
line 7: expected KIND,VALUE,TAG
shop.ir direct
my-tracker.net block
203.0.113.5 direct
127.0.0.1 direct
example.com proxy
block
cannot read /nonexistent/rules.txt
One proxy server described by a share link: protocol, address, credentials and transport options. ParseLink fills it and NodeToLink writes it back.
Name Type Description validbool = falseFalse when parsing failed; see error. errorstr = ""Why parsing failed. protocolstr = ""vmess, vless, trojan or ss. remarkstr = ""The human name after the # in the link. hoststr = ""The server host name or IP. portint = 0The server port. uuidstr = ""The user id of VMess and VLESS. passwordstr = ""The password of Trojan and Shadowsocks. methodstr = ""The cipher: the Shadowsocks method or the VMess security. alter_idint = 0The VMess alter id, normally 0. flowstr = ""The VLESS flow, such as xtls-rprx-vision. encryptionstr = ""The VLESS encryption field, normally none. networkstr = "tcp"The transport: tcp, ws, grpc and so on. The default is tcp. header_typestr = ""The header obfuscation type of the transport. tlsbool = falseTrue when the link asks for TLS. securitystr = ""none, tls or reality. snistr = ""The server name sent in the TLS handshake. alpnstr = ""The ALPN list, comma separated. fingerprintstr = ""The client fingerprint name, such as chrome. pathstr = ""The WebSocket or HTTP path. host_headerstr = ""The Host header for WebSocket and HTTP transports. service_namestr = ""The gRPC service name. grpc_modestr = ""multi for a gRPC link with mode=multi (or a vmess link with type multi), empty for gun. public_keystr = ""The REALITY public key. short_idstr = ""The REALITY short id. spider_xstr = ""The REALITY spider path. pluginstr = ""The Shadowsocks plugin string, if any. allow_insecurebool = falseTrue when the link says to skip certificate checks. Treat this as a warning sign.
net-tunnel-Link.salam Copy
import net.tunnel
func main :
link := "vless://b831381d-6324-4d53-ad4f-8cda48b30811@203.0.113.9:443?encryption=none&security=tls&sni=cdn.example.com&type=ws&host=cdn.example.com&path=%2Fws#Berlin%201"
n := tunnel. ParseLink (link)
println n.valid, n.protocol, tunnel. NodeAddr (n). String (), n.network, n.security, n.sni, n.path
println tunnel. NodeLabel (n)
println tunnel. NodeToLink (n) == link
println tunnel. ParseLink ( "vless://@host:1" ).valid, tunnel. ParseLink ( "http://x" ).valid
println tunnel. ParseLink ( "ss://aes-128-gcm:secret@10.0.0.1:8388#ss" ).method
text := link + "\n# a comment\ntrojan://pw@t.example.org:443#T\nnot a link\n"
sub := tunnel. ParseSubscription (text)
println sub.nodes. Len (), sub.skipped, tunnel. NodeLabel (sub.nodes[ 1 ])
println tunnel. PctEncode ( "a b/c" ), tunnel. PctDecode ( "%D8%B3%D9%84%D8%A7%D9%85" )
println tunnel. B64EncodeUrl ( "subjects?_d>>" ), tunnel. B64Decode ( "c2FsYW0" )
end Output true vless 203.0.113.9:443 ws tls cdn.example.com /ws
Berlin 1
true
false false
aes-128-gcm
2 1 T
a%20b%2Fc سلام
c3ViamVjdHM_X2Q-Pg salam
The nodes found in a subscription text, plus how many lines were not understood.
Name Type Description nodesVector<Node>The valid nodes in order. skippedint = 0How many non empty, non comment lines were not valid links.
The example for this item is shown with the ones above: net-tunnel-Link.salam
Thread safe counters for a proxy: bytes up and down and connection counts. Make it with NewStats.
Name Type Description upatomic.I64Bytes sent from the client side towards the target. downatomic.I64Bytes sent from the target back to the client. totalatomic.I64Connections handled in total. activeatomic.I64Connections being relayed right now. failedatomic.I64Connections whose target could not be reached.
net-tunnel-Relay.salam Copy
import net.tunnel
import pool
func relay_job (a: dyn tunnel.Stream, b: dyn tunnel.Stream, st: tunnel.Stats):
r := tunnel. RelayWith (a, b, st, 5000 )
println "relayed" , r.up, r.down
end
func main :
near := tunnel. NewPipe ()
far := tunnel. NewPipe ()
client := tunnel. PipeA (near)
left := tunnel. PipeB (near)
right := tunnel. PipeA (far)
server := tunnel. PipeB (far)
stats := tunnel. NewStats ()
workers := pool. NewElastic ( 1 as i64 , 2 as i64 , 2 as i64 )
pool. Submit (workers, (): relay_job (left, right, stats) end )
client. Write ( "ping from the client" )
println server. Read ( 100 )
server. Write ( "pong from the server" )
println client. Read ( 100 )
client. Close ()
pool. Wait (workers)
pool. Stop (workers)
println stats. Summary ()
println stats. Up (), stats. Down ()
tunnel. FreeStats (stats)
end Output ping from the client
pong from the server
relayed 20 20
up=20 down=20 total=0 active=0 failed=0
20 20
The example for this item is shown with the ones above: net-tunnel-Relay.salam
The example for this item is shown with the ones above: net-tunnel-Relay.salam
The example for this item is shown with the ones above: net-tunnel-Relay.salam
Reads the active counter.
The example for this item is shown with the ones above: net-tunnel-Relay.salam
Reads the failed counter.
The example for this item is shown with the ones above: net-tunnel-Relay.salam
Returns all counters as one line, up=... down=... total=... active=... failed=....
The example for this item is shown with the ones above: net-tunnel-Relay.salam
How many bytes Relay moved in each direction.
Name Type Description upi64 = 0Bytes from the first stream to the second. downi64 = 0Bytes from the second stream to the first.
The example for this item is shown with the ones above: net-tunnel-Relay.salam
Remembers when traffic last moved and says whether a connection has been quiet too long.
Name Type Description lastatomic.I64The time of the last activity, in milliseconds. limiti64 = 0How long it may be quiet, in milliseconds. 0 means no limit.
net-tunnel-Idle.salam Copy
import net.tunnel
import sync
func main :
timer := tunnel. NewIdleTimer ( 100 as i64 )
println timer. Expired ()
sync. SleepMs ( 160 )
println timer. Expired (), timer. IdleMs () >= 150
timer. Touch ()
println timer. Expired ()
timer. Free ()
plain := tunnel. NewIdleTimer ( 0 as i64 )
sync. SleepMs ( 30 )
println "no limit:" , plain. Expired ()
plain. Free ()
end Output false
true true
false
no limit: false
The example for this item is shown with the ones above: net-tunnel-Idle.salam
Milliseconds since the last activity.
The example for this item is shown with the ones above: net-tunnel-Idle.salam
True when the limit is set and the idle time is over it.
The example for this item is shown with the ones above: net-tunnel-Idle.salam
The example for this item is shown with the ones above: net-tunnel-Idle.salam
A two way byte connection. Plain TCP, TLS, WebSocket and in memory pipes all implement it, so protocols and relays work on any of them.
net-tunnel-Stream.salam Copy
import net.tunnel
import mem
func main :
pair := tunnel. NewPipe ()
a := tunnel. PipeA (pair)
b := tunnel. PipeB (pair)
println tunnel. WriteAll (a, "HELLO world\nsecond line\n" )
println tunnel. ReadByte (b)
println tunnel. ReadUntil (b, "\n" , 100 ). Len ()
println tunnel. ReadUntil (b, "\n" , 100 )
a. Write ( "12345678" )
buf := mem. Allocate ( 8 as u64 )
println tunnel. ReadFull (b, buf, 8 ), (buf as u8 *)[ 0 ], (buf as u8 *)[ 7 ]
mem. Free (buf)
a. WriteBytes ( "abc" as void *, 3 )
w := tunnel. WithPrefix (b, "<" )
println w. Read ( 4 )
println w. Read ( 10 )
b. SetDeadline ( 50 )
println "nothing to read: [" + b. Read ( 10 ) + "]"
println a. Ok (), b. Ok ()
a. Close ()
println a. Ok (), b. Read ( 10 ). Len ()
dead := tunnel.ClosedStream {}
println dead. Ok (), dead. Write ( "x" ) > 0 , dead. Read ( 10 ). Len ()
end Output true
72
11
second line
true 49 56
<
abc
nothing to read: []
true true
true 0
false false 0
A Stream over a plain TCP connection. WrapTcp makes one.
Name Type Description conntcp.ConnThe TCP connection.
net-tunnel-Wrap.salam Copy
import net.tcp
import net.tunnel
import net.websocket
import sync
mut tcp_port := 0
mut ws_port := 0
mut ready := sync. NewWaitGroup ()
func tcp_server ():
srv := tcp. Bind ( 0 )
tcp_port = tcp. Port (srv)
sync. Done (ready)
c := tcp. Accept (srv)
s := tunnel. WrapTcp (c)
s. Write ( "tcp:" + s. Read ( 100 ))
s. Close ()
end
func ws_server ():
srv := websocket. Listen ( 0 )
ws_port = websocket. Port (srv)
sync. Done (ready)
c := websocket. Accept (srv)
s := tunnel. WrapWebSocket (c)
first := s. Read ( 5 )
rest := s. Read ( 100 )
s. Write ( "ws:" + first + rest)
s. Close ()
end
func main :
sync. Add (ready, 2 as i64 )
t1 := spawn (tcp_server)
t2 := spawn (ws_server)
sync. Wait (ready)
plain := tunnel. DialTcp ( "127.0.0.1" , tcp_port, 2000 )
plain. Write ( "hello" )
println plain. Read ( 100 )
plain. Close ()
conn := websocket. Dial ( "127.0.0.1" , ws_port, "/" )
stream := tunnel. WrapWebSocket (conn)
tunnel. WriteAll (stream, "hello world" )
println tunnel. ReadByte (stream)
println stream. Read ( 100 )
stream. Close ()
join (t1)
join (t2)
end Output tcp:hello
119
s:hello world
Reads text from the stream; see Read.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
func ReadBytes (buf: void *, max: int ): int Reads bytes into a buffer; see ReadBytes.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
func Write (data: str ): int The example for this item is shown with the ones above: net-tunnel-Wrap.salam
func WriteBytes (ptr: void *, n: int ): int Writes bytes; see WriteBytes.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
func SetDeadline (millis: int ) Sets the read and write timeout in milliseconds.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
Stops both directions; see Shutdown.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
Shuts down the sending side of the socket and keeps reading.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
True while the stream is usable.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
A Stream over a TLS connection. WrapTls makes one.
Name Type Description conntls.ConnThe TLS connection.
Reads text from the stream; see Read.
func ReadBytes (buf: void *, max: int ): int Reads bytes into a buffer; see ReadBytes.
func Write (data: str ): int
func WriteBytes (ptr: void *, n: int ): int Writes bytes; see WriteBytes.
func SetDeadline (millis: int ) Sets the read and write timeout in milliseconds.
Stops both directions; see Shutdown.
Sends close_notify and shuts down the sending side. Reads keep working.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
True while the stream is usable.
A Stream over a WebSocket connection. WrapWebSocket makes one.
Name Type Description connwebsocket.ConnThe WebSocket connection. pendingvoid*A message that was only partly read. pending_lenint = 0The size of the partly read message. pending_offint = 0How much of it has been read. timeoutint = -1The timeout in milliseconds, or -1 for none.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
Reads text from the stream; see Read.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
func ReadBytes (buf: void *, max: int ): int Reads bytes into a buffer; see ReadBytes.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
func Write (data: str ): int The example for this item is shown with the ones above: net-tunnel-Wrap.salam
func WriteBytes (ptr: void *, n: int ): int Writes bytes; see WriteBytes.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
func SetDeadline (millis: int ) Sets the read and write timeout in milliseconds.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
Stops both directions; see Shutdown.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
WebSocket has no half close, so this shuts down the whole connection.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
True while the stream is usable.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
A stream that is already closed: reads return nothing and writes fail. A failed dial returns this instead of nothing.
Name Type Description unusedint = 0Not used.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func Read (_max: int ): str Reads text from the stream; see Read.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func ReadBytes (_buf: void *, _max: int ): int Reads bytes into a buffer; see ReadBytes.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func Write (_data: str ): int The example for this item is shown with the ones above: net-tunnel-Stream.salam
func WriteBytes (_ptr: void *, _n: int ): int Writes bytes; see WriteBytes.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func SetDeadline (millis: int ) Sets the read and write timeout in milliseconds.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
Stops both directions; see Shutdown.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
The example for this item is shown with the ones above: net-tunnel-Stream.salam
The example for this item is shown with the ones above: net-tunnel-Stream.salam
True while the stream is usable.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
A set of domains: exact names, suffixes, keywords and regular expressions. Lookups on exact names and suffixes are hashed, so a list of a hundred thousand names stays fast.
Name Type Description fullHashMap<str, bool>Exact names. suffixHashMap<str, bool>Suffixes; a name matches itself and every sub domain. keywordsVector<str>Words that may appear anywhere in a name. regexesVector<str>Regular expressions. countint = 0How many entries were added.
The example for this item is shown with the ones above: net-tunnel-Router.salam
A set of IPv4 and IPv6 networks. A test costs at most one lookup per prefix length in use.
Name Type Description v4HashMap<i64, bool>IPv4 networks by prefix length and address. v4_lensi64 = 0A bit mask of the IPv4 prefix lengths present. v6HashMap<str, bool>IPv6 networks. v6_lensVector<int>The IPv6 prefix lengths present. countint = 0How many networks were added.
The example for this item is shown with the ones above: net-tunnel-Router.salam
Something that opens an outgoing connection to a target and returns it as a Stream. Direct, blocking, proxied and routed dialers all implement it.
net-tunnel-Dialers.salam Copy
import net.tcp
import net.tunnel
import sync
mut port := 0
mut ready := sync. NewWaitGroup ()
func echo_once ():
srv := tcp. Bind ( 0 )
port = tcp. Port (srv)
sync. Done (ready)
c := tcp. Accept (srv)
data := tcp. Read (c, 64 )
tcp. Write (c, "echo:" + data)
tcp. Close (c)
end
func main :
sync. Add (ready, 1 as i64 )
t := spawn (echo_once)
sync. Wait (ready)
target := tunnel. NewAddr ( "127.0.0.1" , port)
blocked := tunnel. Block ()
println blocked. Name (), blocked. Dial (target). Ok ()
mut backup := tunnel. NewFallback ()
tunnel. FallbackAdd (backup, tunnel. Block ())
tunnel. FallbackAdd (backup, tunnel. Direct ())
println backup. Name ()
s := backup. Dial (target)
println s. Ok ()
s. Write ( "hello" )
println s. Read ( 100 )
s. Close ()
join (t)
direct := tunnel.DirectDialer { timeout_ms = 500 }
println direct. Name (), direct. Dial (tunnel. NewAddr ( "127.0.0.1" , tcp. FreePort ())). Ok ()
stream := tunnel. DialTcp ( "127.0.0.1" , tcp. FreePort (), 500 )
println stream. Ok ()
end Output block false
fallback
true
echo:hello
direct false
false
One hop of a proxy chain: it names the server to reach and performs that proxy's handshake over an already open stream. SOCKS, HTTP and Trojan connectors implement it.
net-tunnel-Chain.salam Copy
import net.tcp
import net.tunnel
import sync
mut hop_port := 0
mut hop_log := ""
mut ready := sync. NewWaitGroup ()
struct LineHop :
pub host: str = ""
pub port: int = 0
pub func Server (): tunnel.Addr:
ret tunnel. NewAddr ( this .host, this .port)
end
pub func Handshake (s: dyn tunnel.Stream, target: tunnel.Addr): bool :
s. Write ( "HOP " + target. String () + "\n" )
ret tunnel. ReadUntil (s, "\n" , 50 ) == "OK\n"
end
pub func Name (): str :
ret "linehop"
end
end
func hop_server ():
srv := tcp. Bind ( 0 )
hop_port = tcp. Port (srv)
sync. Done (ready)
c := tcp. Accept (srv)
s := tunnel. WrapTcp (c)
hop_log = tunnel. ReadUntil (s, "\n" , 200 )
s. Write ( "OK\n" )
s. Write ( "end:" + s. Read ( 100 ))
s. Close ()
end
func main :
sync. Add (ready, 1 as i64 )
t := spawn (hop_server)
sync. Wait (ready)
hop := LineHop { host = "127.0.0.1" , port = hop_port }
mut chain := tunnel. NewChain (tunnel. Direct ())
tunnel. ChainAdd (chain, hop)
println chain. Name ()
s := chain. Dial (tunnel. NewAddr ( "service.internal" , 8080 ))
println s. Ok ()
s. Write ( "payload" )
println s. Read ( 100 )
s. Close ()
join (t)
print hop_log
via := tunnel. Via (tunnel. Direct (), hop)
println via. Name ()
end Output chain>linehop
true
end:payload
HOP service.internal:8080
linehop
Connects straight to the target over TCP.
Name Type Description timeout_msint = 10000How long a connect may take, in milliseconds. The default is 10000. resolverdyn HostResolverOptional resolver for domain targets, for example one backed by DNS over HTTPS. has_resolverbool = falseTrue to use the resolver. Each address is tried in turn within the time limit. prefer_v6bool = falseTry IPv6 addresses before IPv4 ones.
The example for this item is shown with the ones above: net-tunnel-Dialers.salam
func Dial (target: Addr): dyn Stream Opens a TCP connection to the target and wraps it as a stream.
The example for this item is shown with the ones above: net-tunnel-Dialers.salam
The example for this item is shown with the ones above: net-tunnel-Dialers.salam
A dialer that refuses everything. It is what a blackhole outbound becomes.
Name Type Description refusedint = 0How many connections it has refused so far.
The example for this item is shown with the ones above: net-tunnel-Dialers.salam
func Dial (_target: Addr): dyn Stream Counts the attempt and returns a closed stream.
The example for this item is shown with the ones above: net-tunnel-Dialers.salam
The example for this item is shown with the ones above: net-tunnel-Dialers.salam
Dials through one proxy hop: it connects to the hop's server with the upstream dialer and then runs the hop's handshake.
Name Type Description upstreamdyn DialerThe dialer used to reach the hop's server. hopdyn ConnectorThe proxy hop to go through.
The example for this item is shown with the ones above: net-tunnel-Chain.salam
func Dial (target: Addr): dyn Stream Reaches the hop, performs its handshake for the target and returns the stream, or a closed stream when any step fails.
The example for this item is shown with the ones above: net-tunnel-Chain.salam
A label made from the hop's name.
The example for this item is shown with the ones above: net-tunnel-Chain.salam
A connector that does nothing. It is only the default value of a field.
Name Type Description unusedint = 0Not used. A struct needs at least one field.
Returns an empty address.
func Handshake (_s: dyn Stream, _target: Addr): bool
A dialer that goes through several proxy hops in order, starting from a base dialer. Build it with NewChain and ChainAdd.
Name Type Description basedyn DialerThe dialer that reaches the first hop. hopsVector<dyn Connector>The hops in the order they are used.
The example for this item is shown with the ones above: net-tunnel-Chain.salam
func Dial (target: Addr): dyn Stream Connects to the first hop, asks it to connect to the second, and so on until the target. The stream it returns talks to the target through every hop.
The example for this item is shown with the ones above: net-tunnel-Chain.salam
A label such as chain>http>socks5.
The example for this item is shown with the ones above: net-tunnel-Chain.salam
A dialer that tries several dialers in order and returns the first stream that works.
Name Type Description itemsVector<dyn Dialer>The dialers to try, in order.
The example for this item is shown with the ones above: net-tunnel-Dialers.salam
func Dial (target: Addr): dyn Stream Tries each dialer and returns the first open stream, or a closed stream when all fail.
The example for this item is shown with the ones above: net-tunnel-Dialers.salam
A label made from the names of the dialers.
The example for this item is shown with the ones above: net-tunnel-Dialers.salam
Which destinations a server may reach. The default refuses loopback, private (RFC 1918), carrier grade NAT, link local and metadata (169.254.169.254), unique local IPv6, multicast, broadcast, unspecified and IPv4-mapped forms of these.
Name Type Description allow_privatebool = falseAllows private and loopback targets, but only when loopback_only is true (set by PolicyForBind). loopback_onlybool = falseTrue when the listener is bound to loopback only. allow_cidrsVector<Matcher>Ranges that stay reachable even if special. deny_cidrsVector<Matcher>Ranges that are always refused; they win over every allow entry. allow_suffixesVector<str>Domain suffixes that stay reachable. deny_suffixesVector<str>Domain suffixes that are always refused. resolverdyn HostResolverThe resolver used to look names up before the check. The system resolver when none is set. has_resolverbool = falseTrue when resolver is set.
The verdict of a policy for one target: allowed or not, and the checked addresses to dial.
Name Type Description allowedbool = falseTrue when the target may be reached. reasonstr = ""Why a target was refused. ipsVector<str>The checked addresses; dial these, never the name again. resolvedbool = falseTrue when the name was resolved.
The outcome of a guarded dial.
Name Type Description streamdyn StreamThe connected stream, or a closed one. deniedbool = falseTrue when the policy refused the target. reasonstr = ""Why it was refused.
A dialer that applies a destination policy: it resolves names once, checks every answer and connects to the checked address, so a name cannot change between the check and the dial.
Name Type Description innerdyn DialerThe dialer used for the connection. policyDestPolicyThe destination policy. routerRouterDialerA router dialer used instead of inner. has_routerbool = falseTrue when router is in use.
func DialChecked (target: Addr): GuardResult Dials and reports whether the policy refused the target.
func Dial (target: Addr): dyn Stream Dials under the policy; a refused target gives a closed stream.
One inbound of a v2ray style config: where to listen and which protocol to speak.
Name Type Description tagstr = ""The name other parts of the config use for this inbound. protocolstr = ""The protocol name, such as mixed, socks or http. It picks the registered InboundMaker. listenstr = "127.0.0.1"The address to listen on. The default is 127.0.0.1. portint = 0The port to listen on. A text port in the JSON is accepted too. settingsstr = ""The raw JSON of the settings object, kept as text for the protocol to read. stream_settingsstr = ""The raw JSON of streamSettings for an inbound, kept as text for the protocol to read.
net-tunnel-Config.salam Copy
import net.tunnel
func main :
text := `{
"inbounds": [{"tag": "in", "listen": "127.0.0.1", "port": 10808, "protocol": "mixed"}],
"outbounds": [
{"tag": "proxy", "protocol": "trojan", "settings": {"servers": [{"address": "t.example.org", "port": 443, "password": "pw"}]}},
{"tag": "direct", "protocol": "freedom"},
{"tag": "block", "protocol": "blackhole"}
],
"routing": {"defaultOutbound": "proxy", "rules": [
{"type": "field", "domain": ["domain:ir"], "outboundTag": "direct"},
{"type": "field", "ip": ["geoip:private"], "outboundTag": "direct"},
{"type": "field", "port": "25", "outboundTag": "block"}
]}
}`
c := tunnel. ParseConfig (text)
println c.errors. Len (), c.inbounds. Len (), c.outbounds. Len (), c.rules. Len (), c.default_outbound
println c.inbounds[ 0 ].port, c.outbounds[ 0 ].protocol, c.rules[ 2 ].port
println "problems:" , tunnel. ValidateConfig (c). Len ()
rt := tunnel. BuildRouter (c)
println rt. MatchHost ( "divar.ir" , 443 ),
rt. MatchHost ( "192.168.1.1" , 80 ),
rt. MatchHost ( "mail.example.com" , 25 ),
rt. MatchHost ( "example.com" , 443 )
println tunnel. ParseConfig (tunnel. ConfigToJson (c)).outbounds. Len ()
bad := tunnel. ParseConfig (
`{"outbounds":[{"tag":"a","protocol":"freedom"},{"tag":"a","protocol":"freedom"}],"routing":{"rules":[{"outboundTag":"zzz","domain":["domain:x"]}]}}`
)
each e in tunnel. ValidateConfig (bad):
println e
end
println tunnel. LoadConfig ( "/nonexistent/config.json" ).errors. Len ()
node := tunnel. ParseLink ( "trojan://pw@t.example.org:443?security=tls&sni=t.example.org#T" )
out := tunnel. OutboundFromNode (node, "proxy" )
println out.tag,
out.protocol,
tunnel. NodeToLink (tunnel. NodeFromOutbound (out)) == tunnel. NodeToLink (node)
mut nodes := Vector {} as Vector <tunnel.Node>
nodes. Push (node)
gen := tunnel. ConfigFromNodes (nodes, 7890 )
println gen.inbounds[ 0 ].port, gen.outbounds. Len (), gen.default_outbound, gen.rules. Len ()
end Output 0 1 3 3 proxy
10808 trojan 25
problems: 0
direct direct block proxy
3
duplicate outbound tag a
rule 0 points to unknown outbound 'zzz'
1
proxy trojan false
7890 3 proxy-1 1
One outbound of a config: a tag, a protocol and its settings.
Name Type Description tagstr = ""The name rules use to send traffic here. protocolstr = ""The protocol name. freedom and blackhole are built in; others need a registered OutboundMaker. settingsstr = ""The raw JSON of the settings object. stream_settingsstr = ""The raw JSON of streamSettings: network, security and their options.
The example for this item is shown with the ones above: net-tunnel-Config.salam
One routing rule of a config: conditions and the outbound tag to use when they match.
Name Type Description domainVector<str>Domain conditions such as domain:ir, full:a.com, keyword:bank or regexp:.... ipVector<str>IP conditions: CIDR ranges, single addresses or geoip:private. portstr = ""A port condition such as 25,465 or 8000-8100. outbound_tagstr = ""The tag of the outbound that gets the traffic.
The example for this item is shown with the ones above: net-tunnel-Config.salam
A whole v2ray style config: inbounds, outbounds and routing rules. ParseConfig and LoadConfig produce it.
Name Type Description log_levelstr = "warning"The log level from the config. The default is warning. inboundsVector<InboundConfig>The inbounds in file order. outboundsVector<OutboundConfig>The outbounds in file order. rulesVector<RuleConfig>The routing rules in file order. The first match wins. default_outboundstr = ""The tag used when no rule matches. Without one, the first outbound is used. errorsVector<str>Problems found while parsing, as readable lines. Empty for a good config.
The example for this item is shown with the ones above: net-tunnel-Config.salam
Something that can build a Dialer for one outbound protocol. Register it with RegisterOutbound so configs can use the protocol name.
net-tunnel-Registry.salam Copy
import net.tunnel
struct LoopbackMaker :
pub unused: int = 0
pub func Protocol (): str :
ret "loopback"
end
pub func Make (_cfg: tunnel.OutboundConfig): dyn tunnel.Dialer:
ret tunnel. Direct ()
end
end
struct SilentInbound :
pub unused: int = 0
pub func Protocol (): str :
ret "silent"
end
pub func Make (_cfg: tunnel.InboundConfig): dyn tunnel.InboundProtocol:
ret tunnel.NullProtocol {}
end
end
func main :
println tunnel. HasOutbound ( "freedom" ), tunnel. HasOutbound ( "loopback" )
tunnel. RegisterOutbound (LoopbackMaker {})
tunnel. RegisterInbound (SilentInbound {})
println tunnel. HasOutbound ( "loopback" ), tunnel. HasInbound ( "silent" )
println tunnel. CreateOutbound (tunnel.OutboundConfig { protocol = "loopback" }). Name ()
println tunnel. CreateOutbound (tunnel.OutboundConfig { protocol = "freedom" }). Name ()
println tunnel. CreateOutbound (
tunnel.OutboundConfig { protocol = "nobody-registered-this" }
). Name ()
println tunnel. CreateInbound (tunnel.InboundConfig { protocol = "silent" }). Name ()
c := tunnel. ParseConfig (
`{"outbounds":[{"tag":"lo","protocol":"loopback"},{"tag":"deny","protocol":"blackhole"}],"routing":{"defaultOutbound":"lo","rules":[{"domain":["full:blocked.test"],"outboundTag":"deny"}]}}`
)
rd := tunnel. BuildDialer (c)
println rd. Pick ( "lo" ). Name (), rd. Pick ( "deny" ). Name (), rd. Pick ( "missing" ). Name ()
println rd. Dial (tunnel. NewAddr ( "blocked.test" , 80 )). Ok (), rd.last_tag
end Output true false
true true
direct
direct
block
null
direct block block
false deny
Something that can build an InboundProtocol for one inbound protocol. Register it with RegisterInbound.
The example for this item is shown with the ones above: net-tunnel-Registry.salam
The outcome of an inbound handshake: whether it worked and what the client asked for.
Name Type Description okbool = falseTrue when the handshake succeeded and the target is known. targetAddrThe address the client wants to reach. protocolstr = ""Which protocol the client spoke, such as socks5 or http. userstr = ""The authenticated user name, empty when there was none. commandint = 1CMD_CONNECT or CMD_UDP. initialstr = ""Bytes the client sent after the request that must be forwarded to the target first, such as the body of a plain HTTP request. initial_datavoid*The same as initial for bytes that may contain zeros, such as the first payload after a Trojan request: a block of initial_size bytes that the forwarder writes to the target first and frees. initial_sizeint = 0The number of bytes in initial_data; zero when the session uses initial or has none. errorstr = ""Why the handshake failed, for logs. wrappedbool = falseTrue when the protocol replaced the client stream with its own (for example an encrypted one). The handler must then use stream instead of the stream it was given. streamdyn StreamThe replacement stream, valid only when wrapped is true. refusedbool = falseTrue when the policy refused the target, so the inbound can answer with not allowed or 403.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
interface InboundProtocol The server side of a proxy protocol: it reads a client's request from a stream and answers it. SOCKS, HTTP and Trojan inbounds implement it.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
Takes a client stream and its Session after a successful handshake and serves it. ForwardHandler is the usual one.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
An inbound protocol that refuses everyone. It is the default value of a field.
Name Type Description unusedint = 0Not used.
net-tunnel-Mixed.salam Copy
import net.tunnel
func main :
println tunnel. DetectMixed ( 5 ) == tunnel.MIXED_SOCKS5,
tunnel. DetectMixed ( 4 ) == tunnel.MIXED_SOCKS4
println tunnel. DetectMixed ( 71 ) == tunnel.MIXED_HTTP,
tunnel. DetectMixed ( 22 ) == tunnel.MIXED_UNKNOWN
println tunnel. ByteString ( 65 ), tunnel. ByteString ( 65 ). Len ()
mut mixed := tunnel. NewMixed ()
println mixed. Name (), mixed.has_socks5, mixed.has_http
tunnel. MixedSetSocks5 (mixed, tunnel.NullProtocol {})
tunnel. MixedSetSocks4 (mixed, tunnel.NullProtocol {})
tunnel. MixedSetHttp (mixed, tunnel.NullProtocol {})
println mixed.has_socks5, mixed.has_socks4, mixed.has_http
pair := tunnel. NewPipe ()
pair.a. Write ( "\x05\x01\x00" )
sess := mixed. Handshake (tunnel. PipeB (pair))
println sess.ok, sess.protocol, sess.error
println tunnel.CMD_CONNECT, tunnel.CMD_UDP
end Output true true
true true
A 1
mixed false false
true true true
false socks5 no protocol
1 3
The example for this item is shown with the ones above: net-tunnel-Mixed.salam
func Handshake (_s: dyn Stream): Session The example for this item is shown with the ones above: net-tunnel-Mixed.salam
func Reply (_s: dyn Stream, _sess: Session, _ok: bool ) The example for this item is shown with the ones above: net-tunnel-Mixed.salam
One port for SOCKS4, SOCKS5 and HTTP proxy at once: it looks at the first byte of each connection and hands it to the right protocol. Build it with NewMixed.
Name Type Description socks4dyn InboundProtocolThe SOCKS4 inbound, when set. socks5dyn InboundProtocolThe SOCKS5 inbound, when set. httpdyn InboundProtocolThe HTTP proxy inbound, when set. has_socks4bool = falseTrue after MixedSetSocks4. has_socks5bool = falseTrue after MixedSetSocks5. has_httpbool = falseTrue after MixedSetHttp.
The example for this item is shown with the ones above: net-tunnel-Mixed.salam
The example for this item is shown with the ones above: net-tunnel-Mixed.salam
func Handshake (s: dyn Stream): Session Reads one byte, picks the protocol with DetectMixed, gives it the byte back and lets it handshake. A protocol that was not set is refused.
The example for this item is shown with the ones above: net-tunnel-Mixed.salam
func Reply (s: dyn Stream, sess: Session, ok: bool ) Answers in the protocol the client used.
The example for this item is shown with the ones above: net-tunnel-Mixed.salam
The standard Handler: dials the target with its dialer, answers the client, forwards any initial bytes and then relays both directions until the connection ends.
Name Type Description dialerdyn DialerThe dialer that reaches the target. Use a RouterDialer here to route by rules. protodyn InboundProtocolThe inbound protocol, used to answer the client. statsStatsCounters for connections and bytes. idle_msint = 300000Close a relayed connection after this many milliseconds without traffic. The default is 300000; 0 means never. guardGuardDialerThe guarded dialer used when has_guard is true. has_guardbool = falseTrue when the forwarder dials through guard.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func Handle (s: dyn Stream, sess: Session) Serves one client: dial, reply, relay, update the counters.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
A TCP listener with a worker pool. Start it with ServeBackground or ServeForever.
Name Type Description portint = 0The port it listens on. When you asked for port 0 this is the free port the system picked. hoststr = ""The address it listens on; used to wake the accept loop when stopping. listenertcp.ServerThe underlying TCP listener. runningatomic.FlagA flag that Stop clears. workerspool.PoolThe pool that serves connections. Each client gets a task. handshake_msint = 8000How long a client has to finish its handshake, in milliseconds. The default is 8000. per_sourceint = 0How many connections of one address may be in their handshake at once. 0 means no limit. sourcesSourceTableThe handshakes in progress per address.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
True when the port was bound.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func Serve (proto: dyn InboundProtocol, h: dyn Handler) Accepts connections in a loop, handshakes each with the protocol and gives the successful ones to the handler. Blocks until Stop.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
Stops the accept loop and closes the listener. Connections already being relayed carry on until they end.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
One end of an in memory pipe made by NewPipe. What one end writes the other reads. Good for tests and for joining two pieces of code without a socket.
Name Type Description rx_RingThe buffer this end reads from. tx_RingThe buffer this end writes to. deadlineint = 0The timeout in milliseconds. livebool = falseFalse once the stream has been closed.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
Reads text from the stream; see Read.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func ReadBytes (buf: void *, max: int ): int Reads bytes into a buffer; see ReadBytes.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func Write (data: str ): int The example for this item is shown with the ones above: net-tunnel-Stream.salam
func WriteBytes (ptr: void *, n: int ): int Writes bytes; see WriteBytes.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func SetDeadline (millis: int ) Sets the read and write timeout in milliseconds.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
Stops both directions; see Shutdown.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
Ends the writing side of the pipe. The other end reads the end of the stream.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
The example for this item is shown with the ones above: net-tunnel-Stream.salam
True while the stream is usable.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
The two ends of a pipe. Take them with PipeA and PipeB.
Name Type Description aPipeStreamThe first end. bPipeStreamThe second end.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
Wraps a stream and hands out some bytes first, before anything the stream itself delivers. MixedProtocol uses it to put back the byte it peeked at.
Name Type Description innerdyn StreamThe wrapped stream. prefixstr = ""The bytes to give first. offint = 0How much of the prefix has been given out already.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
Reads text from the stream; see Read.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func ReadBytes (buf: void *, max: int ): int Reads bytes into a buffer; see ReadBytes.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func Write (data: str ): int The example for this item is shown with the ones above: net-tunnel-Stream.salam
func WriteBytes (ptr: void *, n: int ): int Writes bytes; see WriteBytes.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func SetDeadline (millis: int ) Sets the read and write timeout in milliseconds.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
Stops both directions; see Shutdown.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
Passes the half close to the stream inside.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
The example for this item is shown with the ones above: net-tunnel-Stream.salam
True while the stream is usable.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
Functions
func ParseIPv4 (s: str ): Vector < int > Parses dotted IPv4 text into four numbers. Returns an empty vector for anything that is not a strict IPv4 address.
The example for this item is shown with the ones above: net-tunnel-Addr.salam
func ParseIPv6 (text: str ): Vector < int > Parses IPv6 text, including :: and an embedded IPv4 tail, into sixteen numbers. Returns an empty vector on error.
The example for this item is shown with the ones above: net-tunnel-Addr.salam
func ParseIP (s: str ): Vector < int > Parses IPv4 or IPv6 text. The result has 4 or 16 numbers, or none.
The example for this item is shown with the ones above: net-tunnel-Addr.salam
func FormatIP (b: Vector < int >): str Turns 4 or 16 numbers back into text. IPv6 comes out in the shortest :: form.
The example for this item is shown with the ones above: net-tunnel-Addr.salam
True when the text is a valid IPv4 or IPv6 address.
The example for this item is shown with the ones above: net-tunnel-Addr.salam
func NewAddr (host: str , port: int ): Addr Builds an Addr from a host and a port and sets its kind by looking at the host. Brackets around an IPv6 host are accepted.
The example for this item is shown with the ones above: net-tunnel-Addr.salam
func ParseAddr (s: str ): Addr Parses host:port, 1.2.3.4:80 or [::1]:80. A missing or out of range port makes valid false.
The example for this item is shown with the ones above: net-tunnel-Addr.salam
func EncodeSocks (a: Addr): Vector < u8 > Writes an address in the SOCKS5 wire form: a type byte, the host and a two byte port.
net-tunnel-Wire.salam Copy
import net.tunnel
import mem
func hex (v: Vector < u8 >): str :
digits := "0123456789abcdef"
mut out := ""
repeat v. Len () in i:
b := v[i] as int
out += digits. Substr (b / 16 , 1 ) + digits. Substr (b % 16 , 1 )
end
ret out
end
func main :
target := tunnel. NewAddr ( "example.com" , 443 )
socks := tunnel. EncodeSocks (target)
vless := tunnel. EncodeVless (target)
println "socks5" , hex (socks)
println "vless " , hex (vless)
p := tunnel. VectorBytes (socks)
back := tunnel. DecodeSocks (p, socks. Len ())
println back.valid, back. String (), back. size == socks. Len ()
q := tunnel. VectorBytes (vless)
again := tunnel. DecodeVless (q, vless. Len ())
println again.valid, again. String ()
short := tunnel. DecodeSocks (p, 3 )
println "short" , short.valid
mem. Free (p)
mem. Free (q)
pair := tunnel. NewPipe ()
a := tunnel. PipeA (pair)
b := tunnel. PipeB (pair)
tunnel. WriteAllBytes (a, tunnel. VectorBytes (socks), socks. Len ())
println tunnel. ReadSocksAddr (b). String ()
tunnel. WriteAllBytes (a, tunnel. VectorBytes (vless), vless. Len ())
println tunnel. ReadVlessAddr (b). String ()
end Output socks5 030b6578616d706c652e636f6d01bb
vless 01bb020b6578616d706c652e636f6d
true example.com:443 true
true example.com:443
short false
example.com:443
example.com:443
func EncodeVless (a: Addr): Vector < u8 > Writes an address in the VLESS wire form: the port first, then the type byte and the host.
The example for this item is shown with the ones above: net-tunnel-Wire.salam
func DecodeSocks (ptr: void *, n: int ): Addr Reads a SOCKS5 address from memory. valid is false when the bytes are short or wrong; size tells how many were used.
The example for this item is shown with the ones above: net-tunnel-Wire.salam
func DecodeVless (ptr: void *, n: int ): Addr Reads a VLESS address from memory, the same way DecodeSocks does for SOCKS5.
The example for this item is shown with the ones above: net-tunnel-Wire.salam
func VectorBytes (v: Vector < u8 >): void * Copies a vector of bytes into freshly allocated memory and returns the pointer. Free it with mem.Free.
The example for this item is shown with the ones above: net-tunnel-Wire.salam
func ReadSocksAddr (s: dyn Stream): Addr Reads a SOCKS5 address from a stream, byte by byte, and returns it. Stops early when the stream ends.
The example for this item is shown with the ones above: net-tunnel-Wire.salam
func ReadVlessAddr (s: dyn Stream): Addr Reads a VLESS address from a stream.
The example for this item is shown with the ones above: net-tunnel-Wire.salam
func NewRouterDialer (router: Router): RouterDialer Makes a router dialer with no dialers yet.
The example for this item is shown with the ones above: net-tunnel-RouterDialer.salam
func RouterDialerAdd (rd &: RouterDialer, tag: str , d: dyn Dialer) Registers a dialer under a tag.
The example for this item is shown with the ones above: net-tunnel-RouterDialer.salam
func BuildDialer (c: Config): RouterDialer Builds a RouterDialer from a whole Config: its router from the rules, and one dialer for each outbound.
The example for this item is shown with the ones above: net-tunnel-Registry.salam
func NewConnLimits (max_connections: int , max_per_source: int , deadline_ms: int ): ConnLimits Creates the limits of a server: connection cap, handshakes per source and the total handshake deadline.
func LimitsEnter (l: ConnLimits, fd: i64 ): Admit Admits a new connection if the caps allow, and starts its deadline. Close the descriptor when ok is false.
func LimitsHandshakeDone (l: ConnLimits, a &: Admit) Ends the handshake phase of an admitted connection.
func LimitsRelease (l: ConnLimits, a &: Admit) Gives the slot back when the connection ends.
func LimitsActive (l: ConnLimits): i64 How many connections are admitted now.
func NewSourceTable (): SourceTable The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func SourceKey (remote: str ): str The key a client address is counted under: IPv4 as is, an IPv4-mapped IPv6 address as its IPv4 form, any other IPv6 address as its /64 prefix.
net-tunnel-IPv6.salam Copy
import net.tunnel
func main :
println tunnel. SourceKey ( "::ffff:203.0.113.9" ), tunnel. SourceKey ( "2001:db8:1:2::7" )
local := tunnel. ListenLocal6 ( 0 )
println local. Ok ()
local. Stop ()
dual := tunnel. ListenTcpDual ( 0 )
println dual. Ok ()
dual. Stop ()
at := tunnel. ListenTcpAt ( "::1" , 0 )
println at. Ok (), at.host
at. Stop ()
inbound := tunnel. ListenTcpDualInbound ( 0 )
println inbound. Ok ()
inbound. Stop ()
end
func SourceEnter (t: SourceTable, remote: str , limit: int ): bool Counts one more handshake for the address. False when it already has the limit, so the connection should be dropped. A limit of 0 or an empty address always passes.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func SourceLeave (t: SourceTable, remote: str ) Takes one handshake back off the count of the address.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func NewAcceptServer (a: dyn Acceptor, host: str ): AcceptServer Makes an accept server around an acceptor. The host is only used to wake the loop on Stop.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func ListenTcpOn (host: str , port: int ): AcceptServer Listens on one address. Use 127.0.0.1 for a local proxy, ::1 for IPv6 loopback, 0.0.0.0 for every IPv4 interface and :: for IPv4 and IPv6 together.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func ListenTcpDual (port: int ): AcceptServer Listens on ::, so IPv4 and IPv6 clients are both accepted.
The example for this item is shown with the ones above: net-tunnel-IPv6.salam
func ListenLocal6 (port: int ): AcceptServer Listens on ::1 only. Port 0 picks a free port.
The example for this item is shown with the ones above: net-tunnel-IPv6.salam
func ListenLocal (port: int ): AcceptServer Listens on 127.0.0.1 only. Port 0 picks a free port.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func StartAccepting (srv: AcceptServer, proto: dyn InboundProtocol, h: dyn Handler) Starts the accept loop on its own thread.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func NewRule (tag: str ): Rule Returns an empty rule that points at a tag.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RuleDomain (r &: Rule, host: str ) Adds an exact host condition.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RuleSuffix (r &: Rule, suffix: str ) Adds a domain suffix condition. ir matches ir and a.b.ir but not evilir.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RuleKeyword (r &: Rule, word: str ) Adds a condition that the host contains a word.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RuleRegex (r &: Rule, pattern: str ): bool Adds a regular expression condition. False, and nothing added, when the pattern is not valid.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func ParseCidr (text: str ): Matcher Parses 10.0.0.0/8 or a single address into a matcher. Its kind is 0 on error.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RuleCidr (r &: Rule, text: str ): bool Adds a CIDR condition. False when the text is not a valid range.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RulePrivate (r &: Rule) Adds the private and local ranges: 10/8, 172.16/12, 192.168/16, loopback, link local, 100.64/10, their IPv6 counterparts and localhost.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RulePort (r &: Rule, spec: str ): bool Adds port conditions from text such as 80,443,8000-8100. False when any part is bad.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RuleDomainSpec (r &: Rule, spec: str ): bool Adds a domain condition from v2ray style text: domain:, suffix:, full:, keyword:, regexp: or a bare word as a keyword.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RuleIpSpec (r &: Rule, spec: str ): bool Adds an IP condition from text: a CIDR, an address, or geoip:private.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RuleMatches (r: Rule, target: Addr): bool True when a target satisfies a rule. A domain target is tested against domain matchers, an IP target against CIDR matchers.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RuleMatchesResolved (r: Rule, target: Addr, ips: Vector < Vector < int >>): bool Like RuleMatches, but CIDR and geoip conditions also test the given resolved addresses of a domain target.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func SetResolver (rt &: Router, r: dyn HostResolver, ttl_ms: i64 ) Gives a router a resolver and an answer lifetime in milliseconds. After that, a domain target can match CIDR rules.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RuleGeoSite (r &: Rule, name: str ): bool Adds a named domain list to a rule. False when the list is unknown.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RuleGeoIp (r &: Rule, name: str ): bool Adds a named address list to a rule. False when the list is unknown.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func NewRouter (default_tag: str ): Router Returns a router with no rules and the given default tag.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RouterAdd (rt &: Router, r: Rule) Appends a rule. Rules are tried in the order they were added.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func ParseRules (text: str , default_tag: str ): RuleSet Parses a rule list, one rule per line: DOMAIN, DOMAIN-SUFFIX, DOMAIN-KEYWORD, DOMAIN-REGEX, IP-CIDR, IP-CIDR6, GEOIP,PRIVATE, DST-PORT and FINAL, each followed by a value and a tag. Comments start with #.
The example for this item is shown with the ones above: net-tunnel-Rules.salam
func LoadRules (path: str , default_tag: str ): RuleSet Reads a rule file and parses it with ParseRules.
The example for this item is shown with the ones above: net-tunnel-Rules.salam
func IranRouter (direct_tag: str , proxy_tag: str ): Router A ready router for Iran: .ir domains (also the Persian .ایران forms), private and local addresses and a few well known domestic services go to the direct tag, everything else to the proxy tag.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func PctDecode (s: str ): str Decodes %XX escapes. A broken escape is kept as it is.
The example for this item is shown with the ones above: net-tunnel-Link.salam
func PctEncode (s: str ): str Percent encodes text for a link: everything except letters, digits and -_.~.
The example for this item is shown with the ones above: net-tunnel-Link.salam
func B64Decode (s: str ): str Decodes Base64, standard or URL safe, with or without padding. Line breaks are ignored.
The example for this item is shown with the ones above: net-tunnel-Link.salam
func B64EncodeUrl (s: str ): str Encodes text as URL safe Base64 without padding.
The example for this item is shown with the ones above: net-tunnel-Link.salam
func ParseLink (link: str ): Node Parses a vmess://, vless://, trojan:// or ss:// share link into a Node. Check valid.
The example for this item is shown with the ones above: net-tunnel-Link.salam
func NodeToLink (n: Node): str Writes a node as a share link. Parsing it again gives the same node.
The example for this item is shown with the ones above: net-tunnel-Link.salam
func ParseSubscription (text: str ): Subscription Parses a subscription: Base64 or plain text with one link per line. Blank lines and # comments are skipped and bad lines are counted.
The example for this item is shown with the ones above: net-tunnel-Link.salam
func NodeAddr (n: Node): Addr Returns the node's server as an Addr.
The example for this item is shown with the ones above: net-tunnel-Link.salam
func NodeLabel (n: Node): str Returns a display name: the remark, or protocol://host:port when there is none.
The example for this item is shown with the ones above: net-tunnel-Link.salam
Returns counters that start at zero.
The example for this item is shown with the ones above: net-tunnel-Relay.salam
The example for this item is shown with the ones above: net-tunnel-Relay.salam
func NewIdleTimer (limit_ms: i64 ): IdleTimer Returns a timer that has just seen activity.
The example for this item is shown with the ones above: net-tunnel-Idle.salam
func RelayWith (a: dyn Stream, b: dyn Stream, st: Stats, idle_ms: int ): RelayResult Copies data both ways between two streams until one side ends or the idle limit passes, updating the counters. Blocks, then closes both streams.
The example for this item is shown with the ones above: net-tunnel-Relay.salam
func Relay (a: dyn Stream, b: dyn Stream): RelayResult RelayWith without counters or idle limit. Returns how many bytes moved each way.
The example for this item is shown with the ones above: net-tunnel-Relay.salam
func NewDomainSet (): DomainSet Makes an empty domain set.
The example for this item is shown with the ones above: net-tunnel-Router.salam
Makes an empty address set.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func DomainSetFull (s &: DomainSet, host: str ) The example for this item is shown with the ones above: net-tunnel-Router.salam
func DomainSetSuffix (s &: DomainSet, host: str ) The example for this item is shown with the ones above: net-tunnel-Router.salam
func DomainSetKeyword (s &: DomainSet, word: str ) The example for this item is shown with the ones above: net-tunnel-Router.salam
func DomainSetRegex (s &: DomainSet, pattern: str ): bool Adds a regular expression. False when it is invalid.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func DomainSetContains (s: DomainSet, host: str ): bool True when the lower case host name is in the set.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func IpSetAddNet (s &: IpSet, net: Vector < int >, prefix: int ) Adds a network given as numbers and a prefix length.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func IpSetAdd (s &: IpSet, cidr: str ): bool Adds a network or a single address from text such as 10.0.0.0/8. False when it cannot be read.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func IpSetContains (s: IpSet, ip: Vector < int >): bool True when the address, given as 4 or 16 numbers, is inside a network of the set.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func ParseDomainList (text: str , attr: str ): DomainSet Reads a domain list in the v2ray format: one entry per line, domain:, full:, keyword:, regexp:, include:name, comments with # and attributes such as @ads. A second argument keeps only entries with that attribute, or without it when it starts with !.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func ParseIpList (text: str ): IpSet Reads a list of CIDR networks, one per line, with # comments.
The example for this item is shown with the ones above: net-tunnel-Router.salam
Sets the directory searched for lists named in geosite: and geoip: rules. It holds files such as ir.txt, geosite/ir.txt or geoip/ir.txt.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RegisterGeoSite (name: str , set: DomainSet) Registers a domain set under a name.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func RegisterGeoIp (name: str , set: IpSet) Registers an address set under a name.
The example for this item is shown with the ones above: net-tunnel-Router.salam
Forgets every registered list and the directory.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func LookupGeoSite (spec: str ): DomainSet Finds a domain list by name, from the registry, then the directory, then the built in list (ir, category-ir). A name may end in @attribute. Unknown names give an empty set.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func HasGeoSite (spec: str ): bool True when the domain list exists and is not empty.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func LookupGeoIp (name: str ): IpSet Finds an address list by name from the registry or the directory.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func HasGeoIp (name: str ): bool True when the address list exists and is not empty.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func LoadGeoSiteDat (ptr: void *, n: int , names: Vector < str >): int Reads a geosite.dat from memory (the protobuf file of v2ray and its forks) and registers the lists. A vector of names limits what is loaded; empty means all. Returns how many lists were loaded. Truncated data stops cleanly.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func LoadGeoIpDat (ptr: void *, n: int , names: Vector < str >): int Reads a geoip.dat from memory and registers the lists. The reverse match flag is ignored.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func LoadGeoSiteFile (path: str , names: Vector < str >): int Reads a geosite.dat file. Returns the number of lists, or -1 when the file cannot be read.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func LoadGeoIpFile (path: str , names: Vector < str >): int Reads a geoip.dat file. Returns the number of lists, or -1 when the file cannot be read.
The example for this item is shown with the ones above: net-tunnel-Router.salam
func NewChain (base: dyn Dialer): Chain Starts an empty chain on top of a base dialer. With no hops it behaves like the base dialer.
The example for this item is shown with the ones above: net-tunnel-Chain.salam
func ChainAdd (c &: Chain, hop: dyn Connector) Appends a proxy hop to a chain.
The example for this item is shown with the ones above: net-tunnel-Chain.salam
func NewFallback (): Fallback Returns an empty fallback dialer.
The example for this item is shown with the ones above: net-tunnel-Dialers.salam
func FallbackAdd (f &: Fallback, d: dyn Dialer) Appends a dialer to try after the earlier ones.
The example for this item is shown with the ones above: net-tunnel-Dialers.salam
func Direct (): dyn Dialer Returns a DirectDialer as a Dialer.
The example for this item is shown with the ones above: net-tunnel-Dialers.salam
Returns a BlockDialer as a Dialer.
The example for this item is shown with the ones above: net-tunnel-Dialers.salam
func Via (upstream: dyn Dialer, hop: dyn Connector): dyn Dialer Returns a dialer that goes through one hop, starting from the given upstream dialer. A shortcut for a ProxyDialer.
The example for this item is shown with the ones above: net-tunnel-Chain.salam
func Go (fn: i64 , arg: void *): bool Runs a function on a new thread that cleans up after itself. The function takes one pointer and returns nothing. Handshake and connection threads should use this and not spawn, because a thread that is never joined keeps its stack (8 MB of address space) for good.
func NewDestPolicy (): DestPolicy A policy with the safe defaults.
func PolicyAllowPrivate (p &: DestPolicy, enabled: bool ) Turns allow_private on or off.
func PolicyAllowCidr (p &: DestPolicy, text: str ): bool Adds an allowed range; false for a bad range.
func PolicyDenyCidr (p &: DestPolicy, text: str ): bool Adds a refused range; false for a bad range.
func PolicyAllowSuffix (p &: DestPolicy, suffix: str ) Adds an allowed domain suffix.
func PolicyDenySuffix (p &: DestPolicy, suffix: str ) Adds a refused domain suffix.
func PolicySetResolver (p &: DestPolicy, r: dyn HostResolver) Sets the resolver used before the check, for example a DoH resolver.
func IsLoopbackHost (host: str ): bool True for localhost, 127.0.0.0/8 and ::1.
func PolicyForBind (p: DestPolicy, bind_host: str ): DestPolicy Returns the policy to use for a listener on a host: allow_private only counts when the host is loopback.
func IsSpecialIP (b: Vector < int >): bool True for the bytes of an address that must not be reached by default.
func IsPrivateAddress (ip: str ): bool True for a text address that must not be reached by default.
func PolicyCheckBytes (p: DestPolicy, ip: Vector < int >): bool Checks the bytes of an address against a policy.
func PolicyCheckIP (p: DestPolicy, ip: str ): bool Checks a text address against a policy.
func PolicyCheckName (p: DestPolicy, host: str ): bool Checks a domain name against the suffix lists and the localhost rule.
func PolicyPlan (p: DestPolicy, a: Addr, resolve: bool ): Plan Checks a target and, when asked, resolves the name once and checks every answer. Dial ips from the result.
func Guard (inner: dyn Dialer, p: DestPolicy): GuardDialer Wraps a dialer with a destination policy.
func GuardRouter (rd: RouterDialer, p: DestPolicy): GuardDialer Wraps a router dialer with a destination policy; names routed direct are pinned to the checked address.
func RegisterOutbound (m: dyn OutboundMaker) Adds an outbound maker to the registry. A maker for a protocol that is already registered replaces the old one.
The example for this item is shown with the ones above: net-tunnel-Registry.salam
func RegisterInbound (m: dyn InboundMaker) Adds an inbound maker to the registry. A maker for a protocol that is already registered replaces the old one.
The example for this item is shown with the ones above: net-tunnel-Registry.salam
func HasOutbound (name: str ): bool True when an outbound protocol name is registered. freedom and blackhole always are.
The example for this item is shown with the ones above: net-tunnel-Registry.salam
func HasInbound (name: str ): bool True when an inbound protocol name is registered.
The example for this item is shown with the ones above: net-tunnel-Registry.salam
func CreateOutbound (cfg: OutboundConfig): dyn Dialer Builds the dialer for an outbound config. An unknown protocol gives a BlockDialer, so traffic is refused rather than leaked.
The example for this item is shown with the ones above: net-tunnel-Registry.salam
func CreateInbound (cfg: InboundConfig): dyn InboundProtocol Builds the inbound protocol for an inbound config. An unknown protocol gives a NullProtocol.
The example for this item is shown with the ones above: net-tunnel-Registry.salam
func ParseConfig (text: str ): Config Parses v2ray style JSON into a Config. Problems are collected in errors instead of stopping.
The example for this item is shown with the ones above: net-tunnel-Config.salam
func LoadConfig (path: str ): Config Reads a config file and parses it. A file that cannot be read gives a config with one error.
The example for this item is shown with the ones above: net-tunnel-Config.salam
func ValidateConfig (c: Config): Vector < str > Checks a config for duplicate tags, bad ports, rules that point nowhere and the like. Returns one readable line per problem.
The example for this item is shown with the ones above: net-tunnel-Config.salam
func ConfigToJson (c: Config): str Writes a config back as JSON. Parsing the result gives the same config.
The example for this item is shown with the ones above: net-tunnel-Config.salam
func BuildRouter (c: Config): Router Turns the rules of a config into a Router whose default is the config's default outbound.
The example for this item is shown with the ones above: net-tunnel-Config.salam
func OutboundFromNode (n: Node, tag: str ): OutboundConfig Converts a parsed share link ( Node) into an OutboundConfig with the given tag.
The example for this item is shown with the ones above: net-tunnel-Config.salam
func NodeFromOutbound (o: OutboundConfig): Node The reverse of OutboundFromNode: reads an outbound config back into a Node.
The example for this item is shown with the ones above: net-tunnel-Config.salam
func ConfigFromNodes (nodes: Vector <Node>, listen_port: int ): Config Builds a ready config from subscription nodes: one outbound per node, plus a mixed inbound on the given port, direct and block outbounds, and one rule that sends .ir domains and private addresses direct. The first node is the default.
The example for this item is shown with the ones above: net-tunnel-Config.salam
func DetectMixed (first: int ): int Guesses the protocol from the first byte: 5 is SOCKS5, 4 is SOCKS4, a letter is HTTP, anything else is unknown.
The example for this item is shown with the ones above: net-tunnel-Mixed.salam
func ByteString (b: int ): str Makes a one byte text from a number 0 to 255.
The example for this item is shown with the ones above: net-tunnel-Mixed.salam
func NewMixed (): MixedProtocol Returns an empty MixedProtocol. Add protocols with the MixedSet functions.
The example for this item is shown with the ones above: net-tunnel-Mixed.salam
func MixedSetSocks5 (m &: MixedProtocol, p: dyn InboundProtocol) Sets the SOCKS5 inbound of a mixed protocol.
The example for this item is shown with the ones above: net-tunnel-Mixed.salam
func MixedSetSocks4 (m &: MixedProtocol, p: dyn InboundProtocol) Sets the SOCKS4 inbound of a mixed protocol.
The example for this item is shown with the ones above: net-tunnel-Mixed.salam
func MixedSetHttp (m &: MixedProtocol, p: dyn InboundProtocol) Sets the HTTP proxy inbound of a mixed protocol.
The example for this item is shown with the ones above: net-tunnel-Mixed.salam
func ListenTcp (port: int ): TcpInbound Binds a TCP port (0 picks a free one) and returns the listener with its worker pool. Check Ok.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func ListenTcpAt (host: str , port: int ): TcpInbound Like ListenTcp, for a given address such as ::1 or ::.
The example for this item is shown with the ones above: net-tunnel-IPv6.salam
func ListenTcpDualInbound (port: int ): TcpInbound A TcpInbound listening on :: for both families.
The example for this item is shown with the ones above: net-tunnel-IPv6.salam
func ServeBackground (inb: TcpInbound, proto: dyn InboundProtocol, h: dyn Handler) Starts Serve on its own thread and returns at once.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func ServeForever (inb: TcpInbound, proto: dyn InboundProtocol, h: dyn Handler) Runs Serve on the calling thread. It returns only after Stop.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func NewForwarder (dialer: dyn Dialer, proto: dyn InboundProtocol): ForwardHandler Makes a ForwardHandler from a dialer and the inbound protocol that answers clients, with fresh counters.
The example for this item is shown with the ones above: net-tunnel-Inbound.salam
func NewGuardedForwarder (guard: GuardDialer, proto: dyn InboundProtocol): ForwardHandler A forwarder that dials through a GuardDialer and tells the inbound when the policy refused the target.
func WatchHandshake (fd: i64 , total_ms: int ): i64 Starts a total deadline for a descriptor. When it passes, the socket is shut down, however slowly the peer sends. Returns a ticket; 0 when there is nothing to watch.
func WatchDone (ticket: i64 ): bool Ends a watch. False when it already fired or the ticket is unknown. Call it before closing the descriptor.
How many watches have fired.
Makes a pipe with two ends.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func PipeA (p: PipePair): dyn Stream Returns the first end as a Stream.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func PipeB (p: PipePair): dyn Stream Returns the second end as a Stream.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func WrapTcp (c: tcp.Conn): dyn Stream Wraps a TCP connection as a Stream.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
func WrapTls (c: tls.Conn): dyn Stream Wraps a TLS connection as a Stream.
func WrapWebSocket (c: websocket.Conn): dyn Stream Wraps a WebSocket connection as a Stream: bytes written become binary messages, and incoming messages are read as a continuous byte stream.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
func DialTcp (host: str , port: int , timeout_ms: int ): dyn Stream Connects over TCP with a timeout in milliseconds and returns the stream. Check Ok.
The example for this item is shown with the ones above: net-tunnel-Wrap.salam
func WithPrefix (s: dyn Stream, prefix: str ): dyn Stream Returns a stream that gives the prefix first and then whatever the inner stream gives.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func ReadFull (s: dyn Stream, buf: void *, n: int ): bool Reads exactly the given number of bytes into a buffer, looping over short reads. False when the stream ends first.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func WriteAll (s: dyn Stream, data: str ): bool Writes the whole text, looping over short writes. False on failure.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func WriteAllBytes (s: dyn Stream, ptr: void *, n: int ): bool Writes the whole buffer. False on failure.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func ReadByte (s: dyn Stream): int Reads one byte as a number 0 to 255, or -1 at the end of the stream.
The example for this item is shown with the ones above: net-tunnel-Stream.salam
func ReadUntil (s: dyn Stream, delim: str , max: int ): str Reads until the delimiter text appears or the maximum is reached. The delimiter is included.
The example for this item is shown with the ones above: net-tunnel-Stream.salam