╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
223 protocol_version: xit.net_server_common.ProtocolVersion, 224 repo: []const u8, 225 226 fn deinit(self: SshRequest, allocator: std.mem.Allocator) void { 227 allocator.free(self.repo); 228 } 229 }; 230 231 fn runSshListener( 232 comptime repo_kind: rp.RepoKind, 233 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 234 io: std.Io, 235 allocator: std.mem.Allocator, 236 project_root: []const u8, 237 ssh_listen: []const u8, 238 err: *std.Io.Writer, 239 ) !void { 240 const listen_address = try parseListenAddress(ssh_listen); 241 const address = try std.Io.net.IpAddress.parseIp4(listen_address.host, listen_address.port); 242 var net_server = try address.listen(io, .{ .reuse_address = true }); 243 defer net_server.deinit(io); 244 245 try err.print("serving SSH helper connections on {s}, project root {s}\n", .{ ssh_listen, project_root }); 246 try err.flush(); 247 248 while (true) { 249 const stream = net_server.accept(io) catch |accept_err| { 250 try err.print("ssh accept failed: {s}\n", .{@errorName(accept_err)}); 251 continue; 252 }; 253 defer stream.close(io); 254 255 handleSshConnection(repo_kind, any_repo_opts, io, allocator, project_root, stream) catch |request_err| { 256 try err.print("ssh request failed: {s}\n", .{@errorName(request_err)}); 257 try err.flush(); 258 }; 259 } 260 } 261 262 fn handleSshConnection( 263 comptime repo_kind: rp.RepoKind, 264 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 265 io: std.Io, 266 allocator: std.mem.Allocator, 267 project_root: []const u8, 268 stream: std.Io.net.Stream, 269 ) !void { 270 var send_buffer = [_]u8{0} ** any_repo_opts.net_buffer_size; 271 var recv_buffer = [_]u8{0} ** any_repo_opts.net_buffer_size; 272 var reader = stream.reader(io, &recv_buffer); 273 var writer = stream.writer(io, &send_buffer); 274 275 const request = try readSshRequest(allocator, &reader.interface); 276 defer request.deinit(allocator); 277 278 const repo_path = try resolveSshRepoPath(allocator, project_root, request.repo); 279 defer allocator.free(repo_path); 280 281 if (!isSubPath(project_root, repo_path)) return error.Forbidden; 282 283 if (any_repo_opts.hash) |hash_kind| { 284 var repo = try rp.Repo(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind)).open(io, allocator, .{ .path = repo_path }); 285 defer repo.deinit(io, allocator); 286 try runSshService(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind), &repo, io, allocator, &reader.interface, &writer.interface, request); 287 } else { 288 var any_repo = try rp.AnyRepo(repo_kind, any_repo_opts).open(io, allocator, .{ .path = repo_path }); 289 defer any_repo.deinit(io, allocator); 290 switch (any_repo) { 291 inline else => |*repo| { 292 try runSshService(repo.self_repo_kind, repo.self_repo_opts, repo, io, allocator, &reader.interface, &writer.interface, request); 293 }, 294 } 295 } 296 } 297 298 fn runSshService( 299 comptime repo_kind: rp.RepoKind, 300 comptime repo_opts: rp.RepoOpts(repo_kind), 301 repo: *rp.Repo(repo_kind, repo_opts), 302 io: std.Io, 303 allocator: std.mem.Allocator, 304 reader: *std.Io.Reader, 305 writer: *std.Io.Writer, 306 request: SshRequest, 307 ) !void { 308 switch (request.service) { 309 .upload_pack => try repo.uploadPack(io, allocator, reader, writer, .{ 310 .protocol_version = request.protocol_version, 311 }), 312 .receive_pack => try repo.receivePack(io, allocator, reader, writer, .{ 313 .protocol_version = request.protocol_version, 314 }), 315 } 316 } 317 318 fn readSshRequest(allocator: std.mem.Allocator, reader: *std.Io.Reader) !SshRequest { 319 const magic = try readSshPreludeLine(reader); 320 if (!std.mem.eql(u8, magic, "haxy-ssh-helper-v1")) return error.InvalidSshMagic; 321 322 const service_line = try readSshPreludeLine(reader); 323 const service = parseSshService(stripPrefix(service_line, "service=") orelse return error.InvalidSshServiceLine) orelse return error.InvalidSshService; 324 325 const protocol_line = try readSshPreludeLine(reader); 326 const protocol_version = std.meta.stringToEnum( 327 xit.net_server_common.ProtocolVersion, 328 stripPrefix(protocol_line, "protocol=") orelse return error.InvalidSshProtocolLine, 329 ) orelse return error.InvalidSshProtocol; 330 331 const repo_length_line = try readSshPreludeLine(reader); 332 const repo_len = try std.fmt.parseInt(usize, stripPrefix(repo_length_line, "repo-length=") orelse return error.InvalidSshRepoLengthLine, 10); 333 334 const empty_line = try readSshPreludeLine(reader); 335 if (empty_line.len != 0) return error.InvalidSshPreludeTerminator; 336 337 const repo = try reader.readAlloc(allocator, repo_len); 338 errdefer allocator.free(repo); 339 340 return .{ 341 .service = service, 342 .protocol_version = protocol_version, 343 .repo = repo, 344 }; 345 } 346 347 fn readSshPreludeLine(reader: *std.Io.Reader) ![]const u8 { 348 return (try reader.takeDelimiter('\n')) orelse error.InvalidSshHelperRequest; 349 } 350 351 fn parseSshService(value: []const u8) ?SshService { 352 if (std.mem.eql(u8, value, "upload-pack")) return .upload_pack; 353 if (std.mem.eql(u8, value, "receive-pack")) return .receive_pack; 354 return null; 355 } 356 357 fn stripPrefix(value: []const u8, prefix: []const u8) ?[]const u8 { 358 if (!std.mem.startsWith(u8, value, prefix)) return null; 359 return value[prefix.len..]; 360 } 361 362 fn resolveSshRepoPath( 363 allocator: std.mem.Allocator, 364 project_root: []const u8, 365 repo: []const u8, 366 ) ![]const u8 { 367 if (std.fs.path.isAbsolute(repo)) { 368 return try std.fs.path.resolve(allocator, &.{repo}); 369 } 370 return try std.fs.path.resolve(allocator, &.{ project_root, repo }); 371 } 372 373 fn parseListenAddress(value: []const u8) !ListenAddress { 374 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 375 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 376 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 377 return .{ .host = value[0..colon], .port = port }; 378 } 379 380 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 381 if (encoded.len == 0) return error.InvalidRepoPath; 382 383 const mutable = try allocator.dupe(u8, encoded); 384 errdefer allocator.free(mutable); 385 const decoded = std.Uri.percentDecodeInPlace(mutable); 386 387 var iter = std.mem.splitScalar(u8, decoded, '/'); 388 while (iter.next()) |segment| { 389 if (segment.len == 0 or std.mem.eql(u8, segment, ".") or std.mem.eql(u8, segment, "..")) { 390 return error.InvalidRepoPath; 391 } 392 } 393 394 return try allocator.realloc(mutable, decoded.len); 395 } 396 397 fn isSubPath(parent: []const u8, child: []const u8) bool { 398 if (std.mem.eql(u8, parent, std.fs.path.sep_str)) return std.fs.path.isAbsolute(child); 399 if (!std.mem.startsWith(u8, child, parent)) return false; 400 return child.len == parent.len or child[parent.len] == std.fs.path.sep; 401 } 402 403 fn normalizeMethod(method: std.http.Method) std.http.Method { 404 return if (method == .HEAD) .GET else method; 405 } 406 407 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 408 const git_protocol = header orelse return .v0; 409 var version: xit.net_server_common.ProtocolVersion = .v0; 410 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 411 while (iter.next()) |entry| { 412 const value = std.mem.trimStart(u8, entry, " "); 413 if (std.mem.startsWith(u8, value, "version=")) { 414 const v = value["version=".len..]; 415 if (std.mem.eql(u8, v, "2")) { 416 version = .v2; 417 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 418 version = .v1; 419 } 420 } 421 } 422 return version; 423 } 424 425 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 426 var it = request.iterateHeaders(); 427 while (it.next()) |header| { 428 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 429 } 430 return null; 431 } 432 433 fn writeSimpleResponse( 434 http_server: *std.http.Server, 435 code: u16, 436 message: []const u8, 437 content_type: []const u8, 438 body: []const u8, 439 ) !void { 440 try http_server.out.print( 441 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 442 .{ code, message, content_type, body.len, body }, 443 ); 444 } 445