|
| 1 | +//go:build linux || android || (darwin && !ios) |
| 2 | + |
| 3 | +package libbox |
| 4 | + |
| 5 | +import ( |
| 6 | + "github.com/sagernet/sing-box/protocol/tailscale/tailssh" |
| 7 | + "github.com/sagernet/sing/common" |
| 8 | +) |
| 9 | + |
| 10 | +type nativeShellSession struct { |
| 11 | + shell *tailssh.Shell |
| 12 | +} |
| 13 | + |
| 14 | +func OpenNativeShellSession( |
| 15 | + shell, cwd string, |
| 16 | + args, environ StringIterator, |
| 17 | + term string, |
| 18 | + rows, cols, uid, gid int32, |
| 19 | + groups Int32Iterator, |
| 20 | +) (ShellSession, error) { |
| 21 | + sh, err := tailssh.OpenPtyShell( |
| 22 | + shell, |
| 23 | + iteratorToArray[string](args), |
| 24 | + iteratorToArray[string](environ), |
| 25 | + cwd, |
| 26 | + int(uid), int(gid), |
| 27 | + common.Map(iteratorToArray[int32](groups), func(g int32) int { return int(g) }), |
| 28 | + uint16(rows), uint16(cols), |
| 29 | + ) |
| 30 | + if err != nil { |
| 31 | + return nil, err |
| 32 | + } |
| 33 | + return &nativeShellSession{shell: sh}, nil |
| 34 | +} |
| 35 | + |
| 36 | +func OpenNativePipeSession( |
| 37 | + shell, cwd string, |
| 38 | + args, environ StringIterator, |
| 39 | + uid, gid int32, |
| 40 | + groups Int32Iterator, |
| 41 | +) (ShellSession, error) { |
| 42 | + sh, err := tailssh.OpenSocketpairShell( |
| 43 | + shell, |
| 44 | + iteratorToArray[string](args), |
| 45 | + iteratorToArray[string](environ), |
| 46 | + cwd, |
| 47 | + int(uid), int(gid), |
| 48 | + common.Map(iteratorToArray[int32](groups), func(g int32) int { return int(g) }), |
| 49 | + ) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + return &nativeShellSession{shell: sh}, nil |
| 54 | +} |
| 55 | + |
| 56 | +func (s *nativeShellSession) MasterFD() int32 { |
| 57 | + return int32(s.shell.MasterFD()) |
| 58 | +} |
| 59 | + |
| 60 | +func (s *nativeShellSession) Resize(rows, cols int32) error { |
| 61 | + return s.shell.Resize(uint16(rows), uint16(cols)) |
| 62 | +} |
| 63 | + |
| 64 | +func (s *nativeShellSession) Signal(sig int32) error { |
| 65 | + return s.shell.Signal(int(sig)) |
| 66 | +} |
| 67 | + |
| 68 | +func (s *nativeShellSession) WaitExit() (int32, error) { |
| 69 | + status, err := s.shell.Wait() |
| 70 | + if err != nil { |
| 71 | + return 0, err |
| 72 | + } |
| 73 | + return int32(status), nil |
| 74 | +} |
| 75 | + |
| 76 | +func (s *nativeShellSession) Close() error { |
| 77 | + return s.shell.Close() |
| 78 | +} |
0 commit comments