| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/sylvinus/agent-vm/internal/cage" |
| 10 | ) |
| 11 | |
| 12 | |
| 13 | |
| 14 | type macOSExitCandidate struct { |
| 15 | IP string `json:"ip"` |
| 16 | Fingerprint string `json:"-"` |
| 17 | Run string `json:"-"` |
| 18 | Observed time.Time `json:"-"` |
| 19 | } |
| 20 | |
| 21 | func macOSStaticCandidate(config cage.Config, run, host string, terminal, browser networkObservation) *macOSExitCandidate { |
| 22 | if host == "" || run == "" || terminal.Status != 200 || browser.Status != 200 || host != terminal.IP || host != browser.IP { |
| 23 | return nil |
| 24 | } |
| 25 | if config.ExitPolicy.Mode == "static" && config.ExitPolicy.IP == host { |
| 26 | return nil |
| 27 | } |
| 28 | return &macOSExitCandidate{IP: host, Fingerprint: config.Fingerprint(), Run: run, Observed: time.Now()} |
| 29 | } |
| 30 | |
| 31 | func (s *Server) bindMacOSExit(w http.ResponseWriter, r *http.Request) { |
| 32 | var input struct { |
| 33 | IP string `json:"ip"` |
| 34 | } |
| 35 | if !readJSON(w, r, &input) { |
| 36 | return |
| 37 | } |
| 38 | s.mu.Lock() |
| 39 | sample := s.safety.MacOSExitCandidate |
| 40 | s.mu.Unlock() |
| 41 | if sample == nil || input.IP != sample.IP { |
| 42 | http.Error(w, "请先运行联网检查,取得终端、浏览器与当前代理一致的出口", http.StatusConflict) |
| 43 | return |
| 44 | } |
| 45 | s.launch(w, "settings", func() error { |
| 46 | return s.saveMacOSStaticExit(s.operationContext(), *sample) |
| 47 | }) |
| 48 | } |
| 49 | |
| 50 | func (s *Server) saveMacOSStaticExit(ctx context.Context, sample macOSExitCandidate) error { |
| 51 | cfg, err := loadConfig(s.configPath) |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | if s.macosGuest == nil || sample.IP == "" || cfg.Fingerprint() != sample.Fingerprint || time.Since(sample.Observed) > 10*time.Minute || sample.Observed.After(time.Now()) { |
| 56 | return errors.New("出口采样已过期或节点已变更,请重新运行联网检查") |
| 57 | } |
| 58 | if err := cfg.RequireManagedProxy(s.profile); err != nil { |
| 59 | return err |
| 60 | } |
| 61 | client, err := s.gateClient() |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | state, err := client.Status(ctx) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | if state.RunID != sample.Run || state.Configuration != sample.Fingerprint { |
| 70 | return errors.New("客机或网络配置已变化,请重新运行联网检查") |
| 71 | } |
| 72 | loc, err := probeLocation(ctx, cfg) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | observed := loc.exitLocation() |
| 77 | if observed.IP != sample.IP { |
| 78 | return errors.New("当前出口与三处采样不一致,请重新确认节点") |
| 79 | } |
| 80 | next := (cage.ExitPolicy{Mode: "static"}).BindMissing(observed) |
| 81 | if verdict, message := next.Evaluate(sample.IP, &observed); verdict != cage.CheckPassed { |
| 82 | return errors.New(message) |
| 83 | } |
| 84 | cfg.ExitPolicy = next |
| 85 | if err := cfg.Validate(); err != nil { |
| 86 | return err |
| 87 | } |
| 88 | closed, err := client.Close(ctx, "固定出口已确认,重启客机后重新检测") |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | s.publishGate(closed) |
| 93 | if closed.Mode != cage.GateClosed || closed.RunID != sample.Run || closed.Configuration != sample.Fingerprint { |
| 94 | return errors.New("无法确认当前客机已关闭外网") |
| 95 | } |
| 96 | if err := writePrivate(s.configPath, cfg); err != nil { |
| 97 | return err |
| 98 | } |
| 99 | s.mu.Lock() |
| 100 | s.safety.MacOSExitCandidate = nil |
| 101 | s.safety.Error = "已保存固定出口。请重启 macOS 客机,登录后重新检测。" |
| 102 | s.mu.Unlock() |
| 103 | return nil |
| 104 | } |