跳到主要内容

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

浏览文件

main.go

130 行 · 3.7 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 )
24
25 flag.BoolVar(&readOnly, "R", false, "read-only server")
26 flag.BoolVar(&debugStderr, "e", false, "debug to stderr")
27 flag.Parse()
28
29 debugStream := io.Discard
30 if debugStderr {
31 debugStream = os.Stderr
32 }
33
34 // An SSH server is represented by a ServerConfig, which holds
35 // certificate details and handles authentication of ServerConns.
36 config := &ssh.ServerConfig{
37 PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
38 // Should use constant-time compare (or better, salt+hash) in
39 // a production setting.
40 fmt.Fprintf(debugStream, "Login: %s\n", c.User())
41 if c.User() == "testuser" && string(pass) == "tiger" {
42 return nil, nil
43 }
44 return nil, fmt.Errorf("password rejected for %q", c.User())
45 },
46 }
47
48 privateBytes, err := os.ReadFile("id_rsa")
49 if err != nil {
50 log.Fatal("Failed to load private key", err)
51 }
52
53 private, err := ssh.ParsePrivateKey(privateBytes)
54 if err != nil {
55 log.Fatal("Failed to parse private key", err)
56 }
57
58 config.AddHostKey(private)
59
60 // Once a ServerConfig has been configured, connections can be
61 // accepted.
62 listener, err := net.Listen("tcp", "0.0.0.0:2022")
63 if err != nil {
64 log.Fatal("failed to listen for connection", err)
65 }
66 fmt.Printf("Listening on %v\n", listener.Addr())
67
68 nConn, err := listener.Accept()
69 if err != nil {
70 log.Fatal("failed to accept incoming connection", err)
71 }
72
73 // Before use, a handshake must be performed on the incoming net.Conn.
74 sconn, chans, reqs, err := ssh.NewServerConn(nConn, config)
75 if err != nil {
76 log.Fatal("failed to handshake", err)
77 }
78 log.Println("login detected:", sconn.User())
79 fmt.Fprintf(debugStream, "SSH server established\n")
80
81 // The incoming Request channel must be serviced.
82 go ssh.DiscardRequests(reqs)
83
84 // Service the incoming Channel channel.
85 for newChannel := range chans {
86 // Channels have a type, depending on the application level
87 // protocol intended. In the case of an SFTP session, this is "subsystem"
88 // with a payload string of "<length=4>sftp"
89 fmt.Fprintf(debugStream, "Incoming channel: %s\n", newChannel.ChannelType())
90 if newChannel.ChannelType() != "session" {
91 newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
92 fmt.Fprintf(debugStream, "Unknown channel type: %s\n", newChannel.ChannelType())
93 continue
94 }
95 channel, requests, err := newChannel.Accept()
96 if err != nil {
97 log.Fatal("could not accept channel.", err)
98 }
99 fmt.Fprintf(debugStream, "Channel accepted\n")
100
101 // Sessions have out-of-band requests such as "shell",
102 // "pty-req" and "env". Here we handle only the
103 // "subsystem" request.
104 go func(in <-chan *ssh.Request) {
105 for req := range in {
106 fmt.Fprintf(debugStream, "Request: %v\n", req.Type)
107 ok := false
108 switch req.Type {
109 case "subsystem":
110 fmt.Fprintf(debugStream, "Subsystem: %s\n", req.Payload[4:])
111 if string(req.Payload[4:]) == "sftp" {
112 ok = true
113 }
114 }
115 fmt.Fprintf(debugStream, " - accepted: %v\n", ok)
116 req.Reply(ok, nil)
117 }
118 }(requests)
119
120 root := sftp.InMemHandler()
121 server := sftp.NewRequestServer(channel, root)
122 if err := server.Serve(); err != nil {
123 if err != io.EOF {
124 log.Fatal("sftp server completed with error:", err)
125 }
126 }
127 server.Close()
128 log.Print("sftp client exited session.")
129 }
130}