One call that registers every proxy protocol with net.tunnel, so a v2ray style JSON config runs both sides, with a ready to use runner.
Types
An accepted connection waiting for its TLS or WebSocket upgrade.
Name Type Description rawtransport.ConnThe raw connection. settingstransport.SettingsHow to upgrade it. upgradedbool = falseTrue when the connection was accepted through transport.Accept (gRPC and Reality), so the upgrade has already been done.
net-vpn-App.salam Copy
import net.socks
import net.tcp
import net.tunnel
import net.vpn
mut echo_srv := tcp.Server {}
func echo_once ():
c := tcp. Accept (echo_srv)
s := tunnel. WrapTcp (c)
data := s. Read ( 100 )
s. Write ( "echo:" + data)
s. Close ()
end
func main :
echo_srv = tcp. Bind ( 0 )
_t := spawn (echo_once)
vpn. Register ()
cfg := tunnel. ParseConfig (
`{"inbounds":[{"tag":"in","port":0,"protocol":"mixed","settings":{"allowPrivate":"true"}}],"outbounds":[{"tag":"direct","protocol":"freedom"}]}`
)
stats := tunnel. NewStats ()
listeners := vpn. StartAll (cfg, stats)
println "listening:" , vpn. Ok (listeners[ 0 ])
s := tunnel. DialTcp ( "127.0.0.1" , listeners[ 0 ].port, 3000 )
r := socks. ClientHandshake (
s, socks.Client { version = 5 }, socks.CmdConnect, "127.0.0.1" , tcp. Port (echo_srv)
)
println "socks handshake:" , r.ok
s. Write ( "hello" )
println s. Read ( 100 )
s. Close ()
vpn. Stop (listeners[ 0 ])
tcp. CloseServer (echo_srv)
end Output listening: true
socks handshake: true
echo:hello
True when the accept worked.
The example for this item is shown with the ones above: net-vpn-App.salam
func Open (): dyn tunnel.Stream Runs the upgrade and returns the stream.
The example for this item is shown with the ones above: net-vpn-App.salam
The example for this item is shown with the ones above: net-vpn-App.salam
The address of the client.
The example for this item is shown with the ones above: net-vpn-App.salam
Accepts connections for a transport listener.
Name Type Description listenertransport.ListenerThe listener. settingstransport.SettingsNetwork and TLS settings.
The example for this item is shown with the ones above: net-vpn-App.salam
func Accept (): dyn tunnel.Pending Waits for the next connection.
The example for this item is shown with the ones above: net-vpn-App.salam
The example for this item is shown with the ones above: net-vpn-App.salam
False when listening or loading the certificate failed.
The example for this item is shown with the ones above: net-vpn-App.salam
The example for this item is shown with the ones above: net-vpn-App.salam
Name Type Description servertunnel.AcceptServerThe accept server. has_serverbool = falseFalse for a trojan inbound, which has its own server. trojantrojan.ServerThe trojan server, when the protocol is trojan. portint = 0The port in use. tagstr = ""The inbound tag.
The example for this item is shown with the ones above: net-vpn-App.salam
Settings for Register: fragmenting the TLS hello and DNS over HTTPS.
Name Type Description fragmentbool = falseSplit the TLS hello of outgoing connections into small pieces. fragment_minint = 4Smallest piece in bytes. fragment_maxint = 12Largest piece in bytes. fragment_delay_msint = 5Pause between pieces. fragment_modestr = "tcp"What fragmenting splits: "tcp", "tls" or "both". fragment_snibool = falseAlso cut inside the server name. fragment_writesint = 1How many of the first handshake writes are fragmented. read_timeout_msint = 0Idle read timeout of outgoing connections. 0 waits forever. write_timeout_msint = 0Idle write timeout of outgoing connections. 0 waits forever. use_dohbool = falseResolve server names through the resolver below. resolverdoh.ResolverThe DNS over HTTPS resolver.
net-vpn-Register.salam Copy
import net.tunnel
import net.vpn
func main :
vpn. RegisterWith (vpn.Options { fragment = true , fragment_min = 2 , fragment_max = 8 })
each proto in [ "vless" , "vmess" , "trojan" , "socks" , "http" , "mixed" ]:
ib := tunnel. CreateInbound (
tunnel.InboundConfig {
protocol = proto,
settings = `{"clients":[{"id":"0b3a8c6e-1f3d-4d7e-9a50-3f8e6a1c2b44","password":"secret"}],"method":"aes-128-gcm","password":"secret"}`
}
)
println "inbound" , proto, ib. Name ()
end
each proto in [ "vless" , "vmess" , "trojan" , "shadowsocks" , "ss" , "socks" , "http" , "freedom" ]:
ob := tunnel. CreateOutbound (
tunnel.OutboundConfig {
protocol = proto, settings = `{"address":"vpn.example.com","port":443}`
}
)
println "outbound" , proto, ob. Name ()
end
cfg := tunnel. ParseConfig (
`{"inbounds":[{"tag":"in","port":0,"protocol":"mixed"}],"outbounds":[{"tag":"direct","protocol":"freedom"},{"tag":"out","protocol":"socks","settings":{"address":"127.0.0.1","port":1080}}]}`
)
dialer := vpn. BuildDialer (cfg)
println dialer. Name ()
vpn. Register ()
println tunnel. CreateInbound (tunnel.InboundConfig { protocol = "vless" }). Name ()
end Output inbound vless vless
inbound vmess vmess
inbound trojan trojan
inbound socks socks5
inbound http http
inbound mixed mixed
outbound vless vless
outbound vmess vmess
outbound trojan trojan
outbound shadowsocks shadowsocks
outbound ss shadowsocks
outbound socks socks5
outbound http http
outbound freedom direct
router
vless
Adapts a net.doh resolver to the router and the direct dialer.
Name Type Description resolverdoh.ResolverThe DNS over HTTPS resolver. ipv6bool = trueAlso ask for AAAA records.
func Lookup (host: str ): tunnel.LookupResult Returns the A and AAAA answers of a name.
Opens a plain transport connection to the target.
Name Type Description settingstransport.SettingsNetwork, TLS and fragment settings.
net-vpn-Settings.salam Copy
import net.transport
import net.tunnel
import net.vpn
func main :
raw := `{"network":"ws","security":"tls","tlsSettings":{"serverName":"cdn.example.com"},"wsSettings":{"path":"/chat","headers":{"Host":"front.example.com"}}}`
s := vpn. StreamSettings (raw)
println s.network, s.tls, s.sni, s.path, s.host
node := tunnel.Node {
protocol = "vless" ,
host = "vpn.example.com" ,
port = 443 ,
network = "ws" ,
tls = true ,
sni = "vpn.example.com" ,
path = "/ws"
}
n := vpn. NodeSettings (node)
println n.network, n.tls, n.sni, n.path
each proto in [ "vless" , "vmess" , "shadowsocks" , "trojan" , "socks" , "http" , "wireguard" ]:
println proto,
vpn. DialerFromNode (
tunnel.Node { protocol = proto, host = "vpn.example.com" , port = 443 }
). Name ()
end
t := vpn. ToTunnelAddr (transport. NewAddr ( "example.com" , 443 ))
println t.host, t.port
back := vpn. ToTransportAddr (tunnel. NewAddr ( "example.org" , 80 ))
println back.host, back.port
end Output ws true cdn.example.com /chat front.example.com
ws true vpn.example.com /ws
vless vless
vmess vmess
shadowsocks shadowsocks
trojan trojan
socks socks5
http http
wireguard block
example.com 443
example.org 80
func Dial (target: tunnel.Addr): dyn tunnel.Stream Connects and returns the stream.
The example for this item is shown with the ones above: net-vpn-Settings.salam
A dialer for a VLESS server.
Name Type Description nodetunnel.NodeThe server.
The example for this item is shown with the ones above: net-vpn-Settings.salam
func Dial (target: tunnel.Addr): dyn tunnel.Stream Connects through the server to the target.
The example for this item is shown with the ones above: net-vpn-Settings.salam
A dialer for a VMess server.
Name Type Description nodetunnel.NodeThe server.
The example for this item is shown with the ones above: net-vpn-Settings.salam
func Dial (target: tunnel.Addr): dyn tunnel.Stream Connects through the server to the target.
The example for this item is shown with the ones above: net-vpn-Settings.salam
A dialer for a Shadowsocks server.
Name Type Description nodetunnel.NodeThe server.
The example for this item is shown with the ones above: net-vpn-Settings.salam
func Dial (target: tunnel.Addr): dyn tunnel.Stream Connects through the server to the target.
The example for this item is shown with the ones above: net-vpn-Settings.salam
Builds the dialer of an outbound of one protocol from a config.
Name Type Description protostr = ""The protocol name.
The example for this item is shown with the ones above: net-vpn-Register.salam
Returns the protocol name.
The example for this item is shown with the ones above: net-vpn-Register.salam
func Make (cfg: tunnel.OutboundConfig): dyn tunnel.Dialer The example for this item is shown with the ones above: net-vpn-Register.salam
Name Type Description usersVector<vless.User>The allowed users.
The example for this item is shown with the ones above: net-vpn-Register.salam
The example for this item is shown with the ones above: net-vpn-Register.salam
func Handshake (s: dyn tunnel.Stream): tunnel.Session Reads the request and checks the user.
func Reply (_s: dyn tunnel.Stream, sess: tunnel.Session, ok: bool ) Closes the stream when the target could not be reached.
Name Type Description usersVector<vmess.User>The allowed users. replayvmess.ReplayFilterRemembers recent requests so they cannot be replayed.
The example for this item is shown with the ones above: net-vpn-Register.salam
The example for this item is shown with the ones above: net-vpn-Register.salam
func Handshake (s: dyn tunnel.Stream): tunnel.Session Reads the request and checks the user.
func Reply (_s: dyn tunnel.Stream, sess: tunnel.Session, ok: bool ) Closes the stream when the target could not be reached.
Accepts Shadowsocks clients.
Name Type Description servershadowsocks.ServerThe Shadowsocks server settings.
The example for this item is shown with the ones above: net-vpn-Register.salam
The example for this item is shown with the ones above: net-vpn-Register.salam
func Handshake (s: dyn tunnel.Stream): tunnel.Session
func Reply (_s: dyn tunnel.Stream, sess: tunnel.Session, ok: bool ) Closes the stream when the target could not be reached.
Builds the inbound protocol of one name from a config.
Name Type Description protostr = ""The protocol name.
The example for this item is shown with the ones above: net-vpn-Register.salam
Returns the protocol name.
The example for this item is shown with the ones above: net-vpn-Register.salam
func Make (cfg: tunnel.InboundConfig): dyn tunnel.InboundProtocol Builds the inbound protocol from its settings.
The example for this item is shown with the ones above: net-vpn-Register.salam
Functions
func InboundPolicy (ib: tunnel.InboundConfig): tunnel.DestPolicy Builds the destination policy of an inbound from its settings: allowPrivate (honoured only on a loopback listener), allowCidr, denyCidr, allowDomain, denyDomain.
func Start (ib: tunnel.InboundConfig, dialer: dyn tunnel.Dialer, stats: tunnel.Stats): Listener Starts one inbound. It listens on the address of the config, 127.0.0.1 by default. Write 0.0.0.0 to open it to the network.
The example for this item is shown with the ones above: net-vpn-App.salam
func StartRouted (ib: tunnel.InboundConfig, rd: tunnel.RouterDialer, stats: tunnel.Stats): Listener Like Start for a router dialer: names that the router sends direct are resolved once, checked against the inbound policy and dialed at the checked address.
func StartAll (c: tunnel.Config, stats: tunnel.Stats): Vector <Listener> Starts every inbound of a config.
The example for this item is shown with the ones above: net-vpn-App.salam
func Ok (l: Listener): bool True while the inbound listens.
The example for this item is shown with the ones above: net-vpn-App.salam
The example for this item is shown with the ones above: net-vpn-App.salam
func Summary (st: tunnel.Stats): str One line of traffic numbers.
The example for this item is shown with the ones above: net-vpn-App.salam
func Wait (st: tunnel.Stats, every: int , limit: int ) Waits for Ctrl-C or the time limit, printing the summary every few seconds.
The example for this item is shown with the ones above: net-vpn-App.salam
func RunConfig (path: str , every: int , limit: int ): int Loads a config file, checks it, starts everything and waits. Returns the exit code.
The example for this item is shown with the ones above: net-vpn-App.salam
func StreamSettings (raw: str ): transport.Settings Reads a v2ray streamSettings object: network (tcp, ws, httpupgrade, grpc), security (none, tls, reality), tlsSettings, realitySettings (client: serverName, publicKey, shortId, spiderX, fingerprint; server: privateKey, dest, serverNames, shortIds), wsSettings, httpupgradeSettings, grpcSettings (serviceName, multiMode), timeouts (read, write in milliseconds) and fragment (mode, sni, writes, min, max, delay).
The example for this item is shown with the ones above: net-vpn-Settings.salam
func NodeSettings (n: tunnel.Node): transport.Settings The transport settings for the node of a share link, including the Reality keys and the gRPC service name, with the fragment and timeout options of Register.
The example for this item is shown with the ones above: net-vpn-Settings.salam
func DialerFromNode (n: tunnel.Node): dyn tunnel.Dialer Builds the dialer of a node for vless, vmess, trojan, shadowsocks, socks or http. Other protocols give a dialer that refuses.
The example for this item is shown with the ones above: net-vpn-Settings.salam
func ToTunnelAddr (a: transport.Addr): tunnel.Addr Converts a transport address to a tunnel address.
The example for this item is shown with the ones above: net-vpn-Settings.salam
func ToTransportAddr (a: tunnel.Addr): transport.Addr Converts a tunnel address to a transport address.
The example for this item is shown with the ones above: net-vpn-Settings.salam
func RegisterWith (o: Options) Registers every protocol with the given options.
The example for this item is shown with the ones above: net-vpn-Register.salam
Registers outbounds and inbounds for socks, http, trojan, vless, vmess and shadowsocks, so a JSON config runs both sides.
The example for this item is shown with the ones above: net-vpn-Register.salam
func BuildDialer (c: tunnel.Config): tunnel.RouterDialer Builds the router and dialers of a config, and gives the router the DNS over HTTPS resolver when it is on.
The example for this item is shown with the ones above: net-vpn-Register.salam