Skip to content

Commit c961d42

Browse files
committed
Add tailscale_enable_funnel to tailscale.h
1 parent 75f9bc2 commit c961d42

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

tailscale.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern int TsnetGetIps(int sd, char *buf, size_t buflen);
2323
extern int TsnetGetRemoteAddr(int listener, int conn, char *buf, size_t buflen);
2424
extern int TsnetListen(int sd, char* net, char* addr, int* listenerOut);
2525
extern int TsnetLoopback(int sd, char* addrOut, size_t addrLen, char* proxyOut, char* localOut);
26+
extern int TsnetEnableFunnel(int sd, int srvPort);
2627

2728
tailscale tailscale_new() {
2829
return TsnetNewServer();
@@ -106,3 +107,7 @@ int tailscale_loopback(tailscale sd, char* addr_out, size_t addrlen, char* proxy
106107
int tailscale_errmsg(tailscale sd, char* buf, size_t buflen) {
107108
return TsnetErrmsg(sd, buf, buflen);
108109
}
110+
111+
int tailscale_enable_funnel(tailscale sd, int srvPort) {
112+
return TsnetEnableFunnel(sd, srvPort);
113+
}

tailscale.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import (
1414
"net"
1515
"os"
1616
"regexp"
17+
"strconv"
1718
"strings"
1819
"sync"
1920
"syscall"
2021
"unsafe"
2122

2223
"tailscale.com/hostinfo"
24+
"tailscale.com/ipn"
2325
"tailscale.com/tsnet"
2426
"tailscale.com/types/logger"
2527
)
@@ -531,3 +533,44 @@ func TsnetLoopback(sd C.int, addrOut *C.char, addrLen C.size_t, proxyOut *C.char
531533

532534
return 0
533535
}
536+
537+
//export TsnetEnableFunnel
538+
func TsnetEnableFunnel(sd C.int, srvPort C.int) C.int {
539+
s, err := getServer(sd)
540+
if err != nil {
541+
return s.recErr(err)
542+
}
543+
544+
ctx := context.Background()
545+
lc, err := s.s.LocalClient()
546+
if err != nil {
547+
return s.recErr(err)
548+
}
549+
550+
st, err := lc.StatusWithoutPeers(ctx)
551+
if err != nil {
552+
return s.recErr(err)
553+
}
554+
domain := st.CertDomains[0]
555+
556+
hp := ipn.HostPort(net.JoinHostPort(domain, strconv.Itoa(443)))
557+
tcpForward := fmt.Sprintf("127.0.0.1:%d", srvPort)
558+
sc := &ipn.ServeConfig{
559+
TCP: map[uint16]*ipn.TCPPortHandler{
560+
443: {
561+
TCPForward: tcpForward,
562+
TerminateTLS: domain,
563+
},
564+
},
565+
AllowFunnel: map[ipn.HostPort]bool{
566+
hp: true,
567+
},
568+
}
569+
570+
lc.SetServeConfig(ctx, sc)
571+
if !sc.AllowFunnel[hp] {
572+
return s.recErr(fmt.Errorf("libtailscale: failed to enable funnel"))
573+
}
574+
575+
return 0
576+
}

tailscale.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ extern int tailscale_accept(tailscale_listener listener, tailscale_conn* conn_ou
175175
// Returns zero on success or -1 on error, call tailscale_errmsg for details.
176176
extern int tailscale_loopback(tailscale sd, char* addr_out, size_t addrlen, char* proxy_cred_out, char* local_api_cred_out);
177177

178+
// tailscale_enable_funnel creates a funnel for a given server port.
179+
//
180+
// Returns:
181+
// 0 - success
182+
// -1 - other error, details printed to the tsnet logger
183+
extern int tailscale_enable_funnel(tailscale sd, int srvPort);
184+
178185
// tailscale_errmsg writes the details of the last error to buf.
179186
//
180187
// After returning, buf is always NUL-terminated.

0 commit comments

Comments
 (0)