╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const xit = @import("xit"); 3 const rp = xit.repo; 4 const ui = @import("./ui.zig"); 5 const web = @import("./web.zig"); 6 const serve_common = @import("./serve_common.zig"); 7 const serve_ssh_protocol = @import("./serve_ssh_protocol.zig"); 8 const serve_ssh = @import("./serve_ssh.zig"); 9 const serve_http = @import("./serve_http.zig"); 10 const evt = @import("./event.zig"); 11 12 pub const Options = struct { 13 http_listen: ?[]const u8 = "127.0.0.1:8080", 14 ssh_listen: ?[]const u8 = "127.0.0.1:8022", 15 wui_listen: []const u8 = "127.0.0.1:8000", 16 data_dir: []const u8 = ".", 17 git_ssh_prefix: []const u8 = "", 18 fallback_on_address_in_use: bool = false, 19 }; 20 21 const ListenAddress = struct { 22 host: []const u8, 23 port: u16, 24 }; 25 26 const BoundListener = struct { 27 address: ListenAddress, 28 server: std.Io.net.Server, 29 30 fn deinit(self: *BoundListener, io: std.Io) void { 31 self.server.deinit(io); 32 } 33 34 fn port(self: *const BoundListener) u16 { 35 return self.server.socket.address.getPort(); 36 } 37 }; 38 39 const SshService = struct { 40 listener: BoundListener, 41 host_key: serve_ssh_protocol.HostKey, 42 session_handler: serve_ssh.SessionHandler, 43 watchdog: serve_ssh.Watchdog = .{}, 44 }; 45 46 pub fn run( 47 comptime repo_kind: rp.RepoKind, 48 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 49 io: std.Io, 50 allocator: std.mem.Allocator, 51 cwd_path: []const u8, 52 options: Options, 53 err: *std.Io.Writer, 54 runnable: anytype, 55 ) !void { 56 // create the data dir 57 58 const data_dir_path = try std.fs.path.resolve(allocator, &.{ cwd_path, options.data_dir }); 59 defer allocator.free(data_dir_path); 60 61 const data_dir = try std.Io.Dir.cwd().createDirPathOpen(io, data_dir_path, .{}); 62 defer data_dir.close(io); 63 64 // create the repos dir 65 66 const repo_root_path = try std.fs.path.resolve(allocator, &.{ data_dir_path, "repos" }); 67 defer allocator.free(repo_root_path); 68 69 try std.Io.Dir.cwd().createDirPath(io, repo_root_path); 70 71 // the admin repo path is where the user/repo metadata events live 72 73 const admin_repo_path = try std.fs.path.resolve(allocator, &.{ data_dir_path, "admin" }); 74 defer allocator.free(admin_repo_path); 75 76 // create the admin repo on first run 77 { 78 var admin_repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |open_err| switch (open_err) { 79 error.RepoNotFound, error.FileNotFound => try evt.initAdminRepo(io, allocator, admin_repo_path), 80 else => |e| return e, 81 }; 82 defer admin_repo.deinit(io, allocator); 83 } 84 85 // create http listener 86 87 const port_policy: PortPolicy = if (options.fallback_on_address_in_use) .fallback else .exact; 88 var http_maybe: ?BoundListener = if (options.http_listen) |value| blk: { 89 const address = try parseListenAddress(value); 90 break :blk .{ .address = address, .server = try listen(io, address, port_policy) }; 91 } else null; 92 defer if (http_maybe) |*http| http.deinit(io); 93 94 // disk-backed store for web login sessions 95 const session_store = try web.SessionStore.init(io, data_dir); 96 defer session_store.deinit(); 97 98 // create wui listener 99 100 const wui_listen_address = try parseListenAddress(options.wui_listen); 101 var wui_server = try listen(io, wui_listen_address, port_policy); 102 defer wui_server.deinit(io); 103 104 // create ssh listener and its session state 105 106 const git_http_port: ?u16 = if (http_maybe) |*http| http.port() else null; 107 var ssh_maybe: ?SshService = if (options.ssh_listen) |value| blk: { 108 const address = try parseListenAddress(value); 109 var listener = BoundListener{ .address = address, .server = try listen(io, address, port_policy) }; 110 errdefer listener.deinit(io); 111 const ssh_port = listener.port(); 112 break :blk .{ 113 .listener = listener, 114 .host_key = try serve_ssh_protocol.HostKey.loadOrGenerate(io, allocator, data_dir_path), 115 .session_handler = .{ 116 .admin_repo_path = admin_repo_path, 117 .repo_root_path = repo_root_path, 118 .wui_port = wui_server.socket.address.getPort(), 119 .git_http_port = git_http_port, 120 .git_ssh_port = ssh_port, 121 .git_ssh_prefix = options.git_ssh_prefix, 122 .err = err, 123 }, 124 }; 125 } else null; 126 defer if (ssh_maybe) |*ssh| ssh.listener.deinit(io); 127 128 // start task group 129 130 var tasks: std.Io.Group = .init; 131 defer tasks.cancel(io); 132 133 // run listeners, printing the ports actually bound 134 135 const git_ssh_port: ?u16 = if (ssh_maybe) |*ssh| ssh.listener.port() else null; 136 137 if (http_maybe) |*http| { 138 try err.print("serving HTTP on {s}:{d}, repo root {s}\n", .{ http.address.host, http.port(), repo_root_path }); 139 try err.flush(); 140 serve_http.runListener(repo_kind, any_repo_opts, io, allocator, repo_root_path, admin_repo_path, &http.server, &tasks, err); 141 } 142 143 if (ssh_maybe) |*ssh| { 144 try err.print("serving SSH on {s}:{d}, host key ED25519 {s}\n", .{ ssh.listener.address.host, ssh.listener.port(), try ssh.host_key.fingerprint(allocator) }); 145 try err.flush(); 146 serve_ssh.runListener(io, allocator, &ssh.host_key, &ssh.session_handler, &ssh.watchdog, &ssh.listener.server, &tasks, err); 147 } 148 149 try err.print("serving web UI on http://{s}:{d}/\n", .{ wui_listen_address.host, wui_server.socket.address.getPort() }); 150 try err.flush(); 151 152 runWebListener(io, allocator, &wui_server, &tasks, .{ .server = .{ 153 .admin_repo_path = admin_repo_path, 154 .session_store = session_store, 155 .git_http_port = git_http_port, 156 .git_ssh_port = git_ssh_port, 157 .git_ssh_prefix = options.git_ssh_prefix, 158 } }, err); 159 160 if (@TypeOf(runnable) != void) { 161 try runnable.run(wui_server.socket.address.getPort(), git_http_port, git_ssh_port); 162 } else { 163 try tasks.await(io); 164 } 165 } 166 167 // serve just the web UI for a single local repo, running `runnable` (the 168 // local TUI) in the foreground while the listener runs in the background. 169 // when the default port is taken, a random ephemeral port is used instead; 170 // the bound port is passed to `runnable.run`. 171 pub fn runLocal( 172 io: std.Io, 173 allocator: std.mem.Allocator, 174 local: ui.RepoSource, 175 err: *std.Io.Writer, 176 runnable: anytype, 177 ) !void { 178 // always fall back, so another instance viewing a second repo just binds 179 // its own port 180 const wui_listen_address = try parseListenAddress((Options{}).wui_listen); 181 var wui_server = try listen(io, wui_listen_address, .fallback); 182 defer wui_server.deinit(io); 183 184 var tasks: std.Io.Group = .init; 185 defer tasks.cancel(io); 186 187 runWebListener(io, allocator, &wui_server, &tasks, .{ .local = local }, err); 188 189 try runnable.run(wui_server.socket.address.getPort()); 190 } 191 192 fn runWebListener( 193 io: std.Io, 194 allocator: std.mem.Allocator, 195 net_server: *std.Io.net.Server, 196 tasks: *std.Io.Group, 197 host: web.Host, 198 err: *std.Io.Writer, 199 ) void { 200 const Context = struct { 201 io: std.Io, 202 allocator: std.mem.Allocator, 203 host: web.Host, 204 err: *std.Io.Writer, 205 }; 206 207 const handle = struct { 208 fn h(ctx: Context, stream: std.Io.net.Stream) void { 209 defer stream.close(ctx.io); 210 web.handleConnection(ctx.io, ctx.allocator, stream, ctx.host, ctx.err) catch |request_err| { 211 serve_common.logError(ctx.io, ctx.err, "web ui request failed: {s}\n", .{@errorName(request_err)}); 212 }; 213 } 214 }.h; 215 216 serve_common.runListener(io, net_server, tasks, err, "web ui", Context{ 217 .io = io, 218 .allocator = allocator, 219 .host = host, 220 .err = err, 221 }, handle); 222 } 223 224 // what a listener does with a port that is already taken 225 const PortPolicy = enum { 226 exact, 227 // leave it to whoever holds it and take an OS-assigned port instead 228 fallback, 229 }; 230 231 fn listen(io: std.Io, listen_address: ListenAddress, policy: PortPolicy) !std.Io.net.Server { 232 const address = try std.Io.net.IpAddress.parseIp4(listen_address.host, listen_address.port); 233 return switch (policy) { 234 .exact => try address.listen(io, .{}), 235 .fallback => address.listen(io, .{}) catch |listen_err| switch (listen_err) { 236 error.AddressInUse => blk: { 237 const any_port = try std.Io.net.IpAddress.parseIp4(listen_address.host, 0); 238 break :blk try any_port.listen(io, .{}); 239 }, 240 else => |e| return e, 241 }, 242 }; 243 } 244 245 fn parseListenAddress(value: []const u8) !ListenAddress { 246 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 247 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 248 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 249 return .{ .host = value[0..colon], .port = port }; 250 } 251