跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

main.go

154 行 · 4.1 KB历史下载
1// An example SFTP server implementation using the golang SSH package.
2// Serves the whole filesystem visible to the user, and has a hard-coded username and password,
3// so not for real use!
4package main
5
6import (
7 "flag"
8 "fmt"
9 "io"
10 "log"
11 "net"
12 "os"
13
14 "github.com/pkg/sftp"
15 "golang.org/x/crypto/ssh"
16)
17
18// Based on example server code from golang.org/x/crypto/ssh and server_standalone
19func main() {
20 var (
21 readOnly bool
22 debugStderr bool
23 winRoot bool
24 )
25
26 flag.BoolVar(&readOnly, "R", false, "read-only server")
27 flag.BoolVar(&debugStderr, "e", false, "debug to stderr")
28 flag.BoolVar(&winRoot, "wr", false, "windows root")
29
30 flag.Parse()
31
32 debugStream := io.Discard
33 if debugStderr {
34 debugStream = os.Stderr
35 }
36
37 // An SSH server is represented by a ServerConfig, which holds
38 // certificate details and handles authentication of ServerConns.
39 config := &ssh.ServerConfig{
40 PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
41 // Should use constant-time compare (or better, salt+hash) in
42 // a production setting.
43 fmt.Fprintf(debugStream, "Login: %s\n", c.User())
44 if c.User() == "testuser" && string(pass) == "tiger" {
45 return nil, nil
46 }
47 return nil, fmt.Errorf("password rejected for %q", c.User())
48 },
49 }
50
51 privateBytes, err := os.ReadFile("id_rsa")
52 if err != nil {
53 log.Fatal("Failed to load private key", err)
54 }
55
56 private, err := ssh.ParsePrivateKey(privateBytes)
57 if err != nil {
58 log.Fatal("Failed to parse private key", err)
59 }
60
61 config.AddHostKey(private)
62
63 // Once a ServerConfig has been configured, connections can be
64 // accepted.
65 listener, err := net.Listen("tcp", "0.0.0.0:2022")
66 if err != nil {
67 log.Fatal("failed to listen for connection", err)
68 }
69 fmt.Printf("Listening on %v\n", listener.Addr())
70
71 nConn, err := listener.Accept()
72 if err != nil {
73 log.Fatal("failed to accept incoming connection", err)
74 }
75
76 // Before use, a handshake must be performed on the incoming
77 // net.Conn.
78 _, chans, reqs, err := ssh.NewServerConn(nConn, config)
79 if err != nil {
80 log.Fatal("failed to handshake", err)
81 }
82 fmt.Fprintf(debugStream, "SSH server established\n")
83
84 // The incoming Request channel must be serviced.
85 go ssh.DiscardRequests(reqs)
86
87 // Service the incoming Channel channel.
88 for newChannel := range chans {
89 // Channels have a type, depending on the application level
90 // protocol intended. In the case of an SFTP session, this is "subsystem"
91 // with a payload string of "<length=4>sftp"
92 fmt.Fprintf(debugStream, "Incoming channel: %s\n", newChannel.ChannelType())
93 if newChannel.ChannelType() != "session" {
94 newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
95 fmt.Fprintf(debugStream, "Unknown channel type: %s\n", newChannel.ChannelType())
96 continue
97 }
98 channel, requests, err := newChannel.Accept()
99 if err != nil {
100 log.Fatal("could not accept channel.", err)
101 }
102 fmt.Fprintf(debugStream, "Channel accepted\n")
103
104 // Sessions have out-of-band requests such as "shell",
105 // "pty-req" and "env". Here we handle only the
106 // "subsystem" request.
107 go func(in <-chan *ssh.Request) {
108 for req := range in {
109 fmt.Fprintf(debugStream, "Request: %v\n", req.Type)
110 ok := false
111 switch req.Type {
112 case "subsystem":
113 fmt.Fprintf(debugStream, "Subsystem: %s\n", req.Payload[4:])
114 if string(req.Payload[4:]) == "sftp" {
115 ok = true
116 }
117 }
118 fmt.Fprintf(debugStream, " - accepted: %v\n", ok)
119 req.Reply(ok, nil)
120 }
121 }(requests)
122
123 serverOptions := []sftp.ServerOption{
124 sftp.WithDebug(debugStream),
125 }
126
127 if readOnly {
128 serverOptions = append(serverOptions, sftp.ReadOnly())
129 fmt.Fprintf(debugStream, "Read-only server\n")
130 } else {
131 fmt.Fprintf(debugStream, "Read write server\n")
132 }
133
134 if winRoot {
135 serverOptions = append(serverOptions, sftp.WindowsRootEnumeratesDrives())
136 fmt.Fprintf(debugStream, "Windows root enabled\n")
137 }
138
139 server, err := sftp.NewServer(
140 channel,
141 serverOptions...,
142 )
143 if err != nil {
144 log.Fatal(err)
145 }
146 if err := server.Serve(); err != nil {
147 if err != io.EOF {
148 log.Fatal("sftp server completed with error:", err)
149 }
150 }
151 server.Close()
152 log.Print("sftp client exited session.")
153 }
154}