Skip to content

Commit f824560

Browse files
committed
intra: global console
1 parent 9d991b4 commit f824560

2 files changed

Lines changed: 46 additions & 41 deletions

File tree

intra/tun2socks.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,52 @@ const maxMemLimit = 4 * 1024 * 1024 * 1024 // 4GiB
6060

6161
func init() {
6262
// increase garbage collection frequency: archive.is/WQBf7
63-
debug.SetGCPercent(25)
63+
debug.SetGCPercent(50)
6464
debug.SetMemoryLimit(maxMemLimit)
6565
debug.SetPanicOnFault(true)
6666
}
6767

68+
// SetupConsole wires up firestack's logger to bdg.
69+
func SetupConsole(console Console) {
70+
ctx := context.Background()
71+
72+
logch := make(chan bool, 1)
73+
crashch := make(chan bool, 1)
74+
go func() {
75+
logfd := false
76+
if r, c, err := log.NewFilebased(); err == nil {
77+
closeall := func() {
78+
core.Close(c)
79+
core.Close(r)
80+
}
81+
if logfd = console.LogFD(int(r.Fd())); logfd {
82+
log.SetConsole(ctx, c)
83+
context.AfterFunc(ctx, closeall)
84+
} else {
85+
closeall()
86+
}
87+
}
88+
if !logfd {
89+
log.SetConsole(ctx, &clogAdapter{console})
90+
}
91+
log.D("tun: <<< console >>>; log out ok; fd? %t", logfd)
92+
logch <- logfd
93+
}()
94+
95+
go func() {
96+
crashfd := pipeCrashOutput(console)
97+
crashch <- crashfd
98+
log.D("tun: <<< console >>>; crash out ok; fd? %t", crashfd)
99+
}()
100+
101+
logfd := <-logch
102+
crashfd := <-crashch
103+
104+
log.ConsoleReady(ctx)
105+
106+
log.I("tun: <<< console >>>; logger: ok; fds (log? %t / crash? %t)", logfd, crashfd)
107+
}
108+
68109
// Connect creates firestack-administered tunnel.
69110
// `fd` is the TUN device. The tunnel acquires an additional reference to it, which is
70111
// released by Disconnect(), so the caller must close `fd` and Disconnect() to close the TUN device.
@@ -259,15 +300,15 @@ func PanicAtRandom(y bool) {
259300
log.I("tun: panic at random? %t", y)
260301
}
261302

262-
func pipeCrashOutput(bdg Bridge) (ok bool) {
303+
func pipeCrashOutput(c Console) (ok bool) {
263304
r, w, err := os.Pipe()
264305
if err != nil {
265306
log.E("tun: err crash output pipe: %v", err)
266307
return false
267308
}
268309
// core.Close(r) // do not close as r isn't dup'd by client code
269310
defer core.Close(w) // always close as w is dup'd by the runtime
270-
if setCrashFd(w) && bdg.CrashFD(int(r.Fd())) {
311+
if setCrashFd(w) && c.CrashFD(int(r.Fd())) {
271312
return true
272313
}
273314
return false

intra/tunnel.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ type rtunnel struct {
139139
var _ Tunnel = (*rtunnel)(nil)
140140

141141
type clogAdapter struct {
142-
b Bridge
142+
b Console
143143
}
144144

145145
var _ log.Console = (*clogAdapter)(nil)
@@ -189,35 +189,6 @@ func NewTunnel2(fd, linkmtu, tunmtu int, ifaddrs, fakedns string, dtr DefaultDNS
189189

190190
const dualstack = settings.IP46
191191

192-
logch := make(chan bool, 1)
193-
crashch := make(chan bool, 1)
194-
go func() {
195-
logfd := false
196-
if r, c, err := log.NewFilebased(); err == nil {
197-
closeall := func() {
198-
core.Close(c)
199-
core.Close(r)
200-
}
201-
if logfd = bdg.LogFD(int(r.Fd())); logfd {
202-
log.SetConsole(ctx, c)
203-
context.AfterFunc(ctx, closeall)
204-
} else {
205-
closeall()
206-
}
207-
}
208-
if !logfd {
209-
log.SetConsole(ctx, &clogAdapter{bdg})
210-
}
211-
log.D("tun: <<< new >>>; log out ok; fd? %t", logfd)
212-
logch <- logfd
213-
}()
214-
215-
go func() {
216-
crashfd := pipeCrashOutput(bdg)
217-
crashch <- crashfd
218-
log.D("tun: <<< new >>>; crash out ok; fd? %t", crashfd)
219-
}()
220-
221192
natpt := x64.NewNatPt2(ctx)
222193
proxies := ipn.NewProxifier(ctx, dualstack, linkmtu, bdg, bdg)
223194
services := rnet.NewServices(ctx, proxies, bdg, bdg)
@@ -234,14 +205,7 @@ func NewTunnel2(fd, linkmtu, tunmtu int, ifaddrs, fakedns string, dtr DefaultDNS
234205
return nil, err
235206
}
236207

237-
log.D("tun: <<< new >>>; default dns: ok")
238-
239-
logfd := <-logch
240-
crashfd := <-crashch
241-
242-
log.ConsoleReady(ctx)
243-
244-
log.D("tun: <<< new >>>; logger, proxies, svcs, bootstrap: ok; fds (log? %t / crash? %t)", logfd, crashfd)
208+
log.D("tun: <<< new >>>; proxies, svcs, bootstrap: ok")
245209

246210
resolver := dnsx.NewResolver(ctx, fakedns, dtr, bdg, natpt)
247211
resolver.Add(newGoosTransport(ctx, proxies)) // os-resolver; fixed

0 commit comments

Comments
 (0)