Link
#struct LinkOne connection to or from a Trojan peer, over TCP, TLS or WebSocket, with the buffers Trojan needs. Dial returns one; the server makes one for each client. Pass it by reference to the read and write functions.
| Name | Type | Description |
|---|---|---|
kind | int = 0 | LinkTCP, LinkTLS or LinkWS. |
tc | tcp.Conn | The TCP connection, when kind is LinkTCP. |
sc | tls.Conn | The TLS connection, when kind is LinkTLS. |
ws | websocket.Conn | The WebSocket connection, when kind is LinkWS. |
rbuf | compress.ByteBuf | Bytes that were read from the peer but not handed out yet. |
roff | int = 0 | How much of rbuf has been handed out. |
ubuf | compress.ByteBuf | Bytes of a UDP packet that is still being assembled. |
pending | compress.ByteBuf | The Trojan request held back by lazy_header until the first write. |
import mem
import net.tcp
import net.trojan
import sync
mut echo_port := 0
mut web_port := 0
mut web_seen := ""
mut ready := sync.NewWaitGroup()
func echo_server():
srv := tcp.Bind(0)
echo_port = tcp.Port(srv)
sync.Done(ready)
until true:
c := tcp.Accept(srv)
until true:
data := tcp.Read(c, 4096)
if data.Len() == 0:
break
end
tcp.Write(c, "echo:" + data)
end
tcp.Close(c)
end
end
func web_server():
srv := tcp.Bind(0)
web_port = tcp.Port(srv)
sync.Done(ready)
until true:
c := tcp.Accept(srv)
web_seen = tcp.Read(c, 4096)
tcp.Write(
c, "HTTP/1.1 200 OK\r\nContent-Length: 12\r\nConnection: close\r\n\r\nhello probes"
)
tcp.Close(c)
end
end
func start_servers():
sync.Add(ready, 2 as i64)
_a := spawn(echo_server)
_b := spawn(web_server)
sync.Wait(ready)
end
func text_of(p: void*, n: int): str:
buf := mem.Allocate((n + 1) as u64)
mem.Copy(buf, p, n as u64)
(buf as u8*)[n] = 0 as u8
ret buf as str
end
func read_text(l &: trojan.Link, want: int): str:
buf := mem.Allocate(8192 as u64)
defer mem.Free(buf)
mut out := ""
until out.Len() < want:
n := trojan.LinkRead(l, buf, 8192)
if n <= 0:
break
end
out += text_of(buf, n)
end
ret out
end
func main:
start_servers()
mut config := trojan.NewServerConfig("", "")
trojan.AllowPrivate(config, true)
trojan.AddPassword(config, "secret")
trojan.AddPasswordHash(config, trojan.PasswordHash("second"))
println config.hashes.Len(), config.handshake_timeout_ms
server := trojan.ServePlain(config, 0)
println "server up:", server.ok, trojan.ServerPort(server) == server.port
mut client := trojan.NewClientConfig("127.0.0.1", server.port, "secret")
client.use_tls = false
println client.server_port != 443, client.use_ws, client.lazy_header, client.timeout_ms
mut link := trojan.Dial(client, "127.0.0.1", echo_port)
println "dial ok:", trojan.LinkOk(link), link.kind == trojan.LinkTCP
println trojan.LinkWriteStr(link, "hello trojan")
println read_text(link, 17)
big := mem.Allocate(30000 as u64)
mem.Set(big, 97, 30000 as u64)
println trojan.LinkWrite(link, big, 30000)
mut total := 0
until total < 30005:
buf := mem.Allocate(8192 as u64)
n := trojan.LinkRead(link, buf, 8192)
mem.Free(buf)
if n <= 0:
break
end
total += n
end
println "echoed", total
mem.Free(big)
trojan.LinkSetTimeout(link, 1000)
trojan.LinkClose(link)
mut second := trojan.NewClientConfig("127.0.0.1", server.port, "second")
second.use_tls = false
second.lazy_header = true
mut lazy := trojan.Dial(second, "127.0.0.1", echo_port)
println trojan.LinkWriteStr(lazy, "lazy header")
println read_text(lazy, 16)
trojan.LinkClose(lazy)
sync.SleepMs(300)
stats := trojan.Stats(server)
println "stats:", stats.accepted, stats.authed, stats.fallbacks
trojan.Stop(server)
end2 5000 server up: true true true false false 10000 dial ok: true true true echo:hello trojan true echoed 30040 true echo:lazy header stats: 2 2 0