main.mx raw

   1  package main
   2  
   3  // git-lfs HTTP batch and download server for git.smesh.lol.
   4  //
   5  // The object store is the same tree the SSH side writes
   6  // (git-lfs-transfer, invoked through sshd-git on port 2222):
   7  //
   8  //	<repoRoot>/<name>.git/lfs/objects/<xx>/<yy>/<oid>
   9  //
  10  // so an object uploaded over SSH is immediately downloadable over HTTPS, and
  11  // nothing has to be copied between the two.
  12  //
  13  // READ ONLY, matching the git HTTP side: no repository on this host sets
  14  // http.receivepack, so HTTPS can fetch but not push. Uploads are refused here
  15  // for the same reason and `git lfs push` keeps going over SSH, where the key
  16  // in authorized_keys is what authorises it. Serving a writable LFS endpoint
  17  // over HTTPS would hand out a way to fill the disk that the git side
  18  // deliberately does not have.
  19  //
  20  // It exists because gitweb forwards /<repo>.git/* to git-http-backend, which
  21  // understands info/refs, git-upload-pack and git-receive-pack and nothing
  22  // else: /info/lfs fell through to a bare 404, so an HTTPS clone could fetch
  23  // refs and objects but never the LFS content. Caddy routes the LFS paths here.
  24  
  25  import (
  26  	"bytes"
  27  	"fmt"
  28  	"os"
  29  	"syscall"
  30  )
  31  
  32  // config is a sovereign self-mutating holder: a main package has no init(), so
  33  // package-level variables cannot be assigned after startup and the settings
  34  // have to live somewhere a method can write to.
  35  type config struct {
  36  	repoRoot string
  37  	hostURL  string
  38  	maxBatch int64
  39  }
  40  
  41  func (c *config) setRoot(p string) {
  42  	c.repoRoot = p
  43  }
  44  
  45  func (c *config) setHost(h string) {
  46  	c.hostURL = h
  47  }
  48  
  49  func newConfig() (c *config) {
  50  	return &config{
  51  		repoRoot: "/home/git",
  52  		hostURL:  "https://git.smesh.lol",
  53  		maxBatch: 33554432,
  54  	}
  55  }
  56  
  57  type request struct {
  58  	method string
  59  	path   string
  60  	hdrs   map[string]string
  61  	body   []byte
  62  }
  63  
  64  // ── http ────────────────────────────────────────────────────────
  65  
  66  func statusText(code int32) (s string) {
  67  	switch code {
  68  	case 200:
  69  		return "OK"
  70  	case 400:
  71  		return "Bad Request"
  72  	case 403:
  73  		return "Forbidden"
  74  	case 404:
  75  		return "Not Found"
  76  	case 405:
  77  		return "Method Not Allowed"
  78  	case 413:
  79  		return "Payload Too Large"
  80  	case 500:
  81  		return "Internal Server Error"
  82  	}
  83  	return "OK"
  84  }
  85  
  86  func headerGet(req *request, key string) (v string, ok bool) {
  87  	v, ok = req.hdrs[key]
  88  	return v, ok
  89  }
  90  
  91  func contentLength(req *request) (n int64) {
  92  	cls, ok := headerGet(req, "content-length")
  93  	if !ok {
  94  		return 0
  95  	}
  96  	for i := 0; i < len(cls); i++ {
  97  		c := cls[i]
  98  		if c >= '0' && c <= '9' {
  99  			n = n*10 + int64(c-'0')
 100  		}
 101  	}
 102  	return n
 103  }
 104  
 105  func writeAll(fd int32, data []byte) (werr error) {
 106  	written := int32(0)
 107  	for written < len(data) {
 108  		n, err := syscall.Write(fd, data[written:])
 109  		if n > 0 {
 110  			written += n
 111  		}
 112  		if err != nil {
 113  			return err
 114  		}
 115  		if n <= 0 {
 116  			return fmt.Errorf("write stalled")
 117  		}
 118  	}
 119  	return nil
 120  }
 121  
 122  // readRequest reads one request. Headers go into a fixed buffer and the body,
 123  // when there is one, is read as exactly Content-Length bytes. Nothing here
 124  // grows: in moxie a slice that outgrows its capacity does not realloc in place
 125  // and a subslice is a copy, so an accumulating buffer is a quadratic trap and
 126  // a large one is arena pressure.
 127  func readRequest(fd int32, maxBatch int64) (req *request, rerr error) {
 128  	buf := []byte{:65536}
 129  	n := int32(0)
 130  	for {
 131  		if n >= len(buf) {
 132  			return nil, fmt.Errorf("headers too large")
 133  		}
 134  		rn, err := syscall.Read(fd, buf[n:])
 135  		if rn > 0 {
 136  			n += rn
 137  		}
 138  		if bytes.Index(buf[:n], []byte("\r\n\r\n")) >= 0 {
 139  			break
 140  		}
 141  		if err != nil || rn == 0 {
 142  			if n == 0 {
 143  				return nil, fmt.Errorf("empty request")
 144  			}
 145  			break
 146  		}
 147  	}
 148  
 149  	sep := bytes.Index(buf[:n], []byte("\r\n\r\n"))
 150  	skip := int32(4)
 151  	if sep < 0 {
 152  		sep = bytes.Index(buf[:n], []byte("\n\n"))
 153  		skip = 2
 154  	}
 155  	if sep < 0 {
 156  		return nil, fmt.Errorf("no header terminator")
 157  	}
 158  
 159  	lines := bytes.Split(buf[:sep], []byte("\n"))
 160  	if len(lines) == 0 {
 161  		return nil, fmt.Errorf("empty request")
 162  	}
 163  
 164  	reqLine := bytes.TrimRight(lines[0], "\r")
 165  	parts := bytes.SplitN(reqLine, []byte(" "), 3)
 166  	if len(parts) < 2 {
 167  		return nil, fmt.Errorf("bad request line")
 168  	}
 169  
 170  	req = &request{
 171  		method: string(parts[0]),
 172  		path:   string(parts[1]),
 173  		hdrs:   map[string]string{},
 174  	}
 175  
 176  	for i := 1; i < len(lines); i++ {
 177  		line := bytes.TrimRight(lines[i], "\r")
 178  		if len(line) == 0 {
 179  			continue
 180  		}
 181  		idx := bytes.IndexByte(line, ':')
 182  		if idx < 0 {
 183  			continue
 184  		}
 185  		k := bytes.ToLower(bytes.TrimSpace(line[:idx]))
 186  		v := bytes.TrimSpace(line[idx+1:])
 187  		req.hdrs[string(k)] = string(v)
 188  	}
 189  
 190  	cl := contentLength(req)
 191  	if cl > maxBatch {
 192  		return req, fmt.Errorf("body too large")
 193  	}
 194  	if cl > 0 {
 195  		bodyStart := sep + skip
 196  		have := n - bodyStart
 197  		if have < 0 {
 198  			have = 0
 199  		}
 200  		body := []byte{:int32(cl)}
 201  		copied := copy(body, buf[bodyStart:n])
 202  		for copied < len(body) {
 203  			rn, err := syscall.Read(fd, body[copied:])
 204  			if rn > 0 {
 205  				copied += rn
 206  			}
 207  			if err != nil || rn == 0 {
 208  				break
 209  			}
 210  		}
 211  		if copied != len(body) {
 212  			return req, fmt.Errorf("short body")
 213  		}
 214  		req.body = body
 215  	}
 216  	return req, nil
 217  }
 218  
 219  func sendHead(fd int32, code int32, ctype string, clen int64) {
 220  	var out bytes.Buffer
 221  	fmt.Fprintf(&out, "HTTP/1.1 %d %s\r\n", code, statusText(code))
 222  	if ctype != "" {
 223  		fmt.Fprintf(&out, "Content-Type: %s\r\n", ctype)
 224  	}
 225  	if clen >= 0 {
 226  		fmt.Fprintf(&out, "Content-Length: %d\r\n", clen)
 227  	}
 228  	out.WriteString("Connection: close\r\n\r\n")
 229  	writeAll(fd, out.Bytes())
 230  }
 231  
 232  func sendBody(fd int32, code int32, ctype string, body []byte) {
 233  	sendHead(fd, code, ctype, int64(len(body)))
 234  	writeAll(fd, body)
 235  }
 236  
 237  func sendJSON(fd int32, code int32, body []byte) {
 238  	sendBody(fd, code, "application/vnd.git-lfs+json", body)
 239  }
 240  
 241  func sendError(fd int32, code int32, msg string) {
 242  	var out bytes.Buffer
 243  	fmt.Fprintf(&out, `{"message":%s}`, jsonQuote(msg))
 244  	out.WriteByte('\n')
 245  	sendJSON(fd, code, out.Bytes())
 246  }
 247  
 248  // ── json ────────────────────────────────────────────────────────
 249  //
 250  // The batch body is machine-generated by git-lfs and flat, so it is scanned
 251  // rather than parsed: pulling in the decoder only to read two fields per
 252  // object would grow the build closure for no gain.
 253  
 254  func jsonQuote(s string) (q string) {
 255  	var b bytes.Buffer
 256  	b.WriteByte('"')
 257  	for i := 0; i < len(s); i++ {
 258  		c := s[i]
 259  		switch c {
 260  		case '"':
 261  			b.WriteString("\\\"")
 262  		case '\\':
 263  			b.WriteString("\\\\")
 264  		case '\n':
 265  			b.WriteString("\\n")
 266  		case '\r':
 267  			b.WriteString("\\r")
 268  		case '\t':
 269  			b.WriteString("\\t")
 270  		default:
 271  			b.WriteByte(c)
 272  		}
 273  	}
 274  	b.WriteByte('"')
 275  	return b.String()
 276  }
 277  
 278  // jsonFind returns the value of the first "<key>" at or after from, and the
 279  // offset just past it. A string value comes back unquoted, a number as its
 280  // digits. ok is false when the key is not there.
 281  func jsonFind(body []byte, key string, from int32) (val string, next int32, ok bool) {
 282  	needle := []byte("\"" + key + "\"")
 283  	idx := bytes.Index(body[from:], needle)
 284  	if idx < 0 {
 285  		return "", int32(len(body)), false
 286  	}
 287  	i := from + idx + int32(len(needle))
 288  	for i < int32(len(body)) && (body[i] == ' ' || body[i] == ':' || body[i] == '\t') {
 289  		i++
 290  	}
 291  	if i >= int32(len(body)) {
 292  		return "", int32(len(body)), false
 293  	}
 294  	if body[i] == '"' {
 295  		i++
 296  		strStart := i
 297  		for i < int32(len(body)) && body[i] != '"' {
 298  			if body[i] == '\\' && i+1 < int32(len(body)) {
 299  				i++
 300  			}
 301  			i++
 302  		}
 303  		return string(body[strStart:i]), i + 1, true
 304  	}
 305  	numStart := i
 306  	for i < int32(len(body)) && body[i] != ',' && body[i] != '}' && body[i] != ']' {
 307  		i++
 308  	}
 309  	return string(bytes.TrimSpace(body[numStart:i])), i, true
 310  }
 311  
 312  func jsonInt(body []byte, key string, from int32) (n int64, next int32, ok bool) {
 313  	s, nx, found := jsonFind(body, key, from)
 314  	if !found {
 315  		return 0, nx, false
 316  	}
 317  	for i := 0; i < len(s); i++ {
 318  		c := s[i]
 319  		if c >= '0' && c <= '9' {
 320  			n = n*10 + int64(c-'0')
 321  		}
 322  	}
 323  	return n, nx, true
 324  }
 325  
 326  // ── lfs ─────────────────────────────────────────────────────────
 327  
 328  func isHex64(s string) (ok bool) {
 329  	if len(s) != 64 {
 330  		return false
 331  	}
 332  	for i := 0; i < len(s); i++ {
 333  		c := s[i]
 334  		if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') {
 335  			continue
 336  		}
 337  		return false
 338  	}
 339  	return true
 340  }
 341  
 342  // safeRepo accepts only the "<name>.git" form the URLs use, and only names made
 343  // of characters that cannot escape the repository directory.
 344  func safeRepo(urlName string) (name string, ok bool) {
 345  	if len(urlName) <= 4 || urlName[len(urlName)-4:] != ".git" {
 346  		return "", false
 347  	}
 348  	name = urlName[:len(urlName)-4]
 349  	if name == "" || name == "." || name == ".." {
 350  		return "", false
 351  	}
 352  	for i := 0; i < len(name); i++ {
 353  		c := name[i]
 354  		if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') {
 355  			continue
 356  		}
 357  		switch c {
 358  		case '-', '_', '.':
 359  			continue
 360  		}
 361  		return "", false
 362  	}
 363  	return name, true
 364  }
 365  
 366  func objectPath(c *config, name string, oid string) (p string) {
 367  	return c.repoRoot + "/" + name + ".git/lfs/objects/" + oid[:2] + "/" + oid[2:4] + "/" + oid
 368  }
 369  
 370  func objectExists(c *config, name string, oid string) (ok bool) {
 371  	_, err := os.Stat(objectPath(c, name, oid))
 372  	return err == nil
 373  }
 374  
 375  func handleBatch(fd int32, c *config, req *request, name string) {
 376  	if len(req.body) == 0 {
 377  		sendError(fd, 400, "empty batch request")
 378  		return
 379  	}
 380  	op, _, _ := jsonFind(req.body, "operation", 0)
 381  	if op == "upload" {
 382  		sendError(fd, 403, "uploads require SSH: git push over HTTPS is disabled on this server")
 383  		return
 384  	}
 385  	if op != "download" {
 386  		sendError(fd, 400, "unsupported operation")
 387  		return
 388  	}
 389  
 390  	var out bytes.Buffer
 391  	out.WriteString(`{"transfer":"basic","objects":[`)
 392  	pos := int32(0)
 393  	first := true
 394  	for {
 395  		oid, next, found := jsonFind(req.body, "oid", pos)
 396  		if !found {
 397  			break
 398  		}
 399  		pos = next
 400  		size, npos, sok := jsonInt(req.body, "size", pos)
 401  		if sok {
 402  			pos = npos
 403  		}
 404  		if !isHex64(oid) {
 405  			continue
 406  		}
 407  		if !first {
 408  			out.WriteByte(',')
 409  		}
 410  		first = false
 411  		fmt.Fprintf(&out, `{"oid":%s,"size":%d`, jsonQuote(oid), size)
 412  		if objectExists(c, name, oid) {
 413  			fmt.Fprintf(&out, `,"actions":{"download":{"href":%s}}`,
 414  				jsonQuote(c.hostURL+"/"+name+".git/info/lfs/objects/"+oid))
 415  		} else {
 416  			out.WriteString(`,"error":{"code":404,"message":"Object does not exist"}`)
 417  		}
 418  		out.WriteByte('}')
 419  	}
 420  	out.WriteString("]}\n")
 421  	sendJSON(fd, 200, out.Bytes())
 422  }
 423  
 424  func handleObject(fd int32, c *config, oid string, name string) {
 425  	if !isHex64(oid) {
 426  		sendError(fd, 404, "bad object id")
 427  		return
 428  	}
 429  	p := objectPath(c, name, oid)
 430  	st, serr := os.Stat(p)
 431  	if serr != nil {
 432  		sendError(fd, 404, "Object does not exist")
 433  		return
 434  	}
 435  	f, err := os.Open(p)
 436  	if err != nil {
 437  		sendError(fd, 404, "Object does not exist")
 438  		return
 439  	}
 440  	defer f.Close()
 441  	sendHead(fd, 200, "application/octet-stream", st.Size())
 442  	buf := []byte{:1048576}
 443  	for {
 444  		rn, rerr := f.Read(buf)
 445  		if rn > 0 {
 446  			if werr := writeAll(fd, buf[:rn]); werr != nil {
 447  				return
 448  			}
 449  		}
 450  		if rn == 0 || rerr != nil {
 451  			return
 452  		}
 453  	}
 454  }
 455  
 456  func handler(fd int32, c *config, req *request) {
 457  	p := bytes.Trim([]byte(req.path), "/")
 458  	parts := bytes.SplitN(p, []byte("/"), 4)
 459  	if len(parts) < 4 {
 460  		sendError(fd, 404, "not found")
 461  		return
 462  	}
 463  	if string(parts[1]) != "info" || string(parts[2]) != "lfs" {
 464  		sendError(fd, 404, "not found")
 465  		return
 466  	}
 467  	name, ok := safeRepo(string(parts[0]))
 468  	if !ok {
 469  		sendError(fd, 404, "no such repository")
 470  		return
 471  	}
 472  
 473  	rest := string(parts[3])
 474  	if rest == "objects/batch" {
 475  		if req.method != "POST" {
 476  			sendError(fd, 405, "batch requires POST")
 477  			return
 478  		}
 479  		handleBatch(fd, c, req, name)
 480  		return
 481  	}
 482  	if len(rest) > 8 && rest[:8] == "objects/" {
 483  		if req.method != "GET" {
 484  			sendError(fd, 405, "object requires GET")
 485  			return
 486  		}
 487  		handleObject(fd, c, rest[8:], name)
 488  		return
 489  	}
 490  	if len(rest) > 7 && rest[len(rest)-7:] == "/verify" {
 491  		// Nothing to verify: an object is only ever stored by the SSH side,
 492  		// which hashes what it writes before moving it into place.
 493  		sendJSON(fd, 200, []byte("{}\n"))
 494  		return
 495  	}
 496  	sendError(fd, 404, "not found")
 497  }
 498  
 499  // ── main ────────────────────────────────────────────────────────
 500  
 501  func parseListen(addr string) (ip [4]byte, port int32) {
 502  	ip = [4]byte{127, 0, 0, 1}
 503  	port = 3100
 504  	i := bytes.LastIndexByte([]byte(addr), ':')
 505  	if i < 0 {
 506  		return ip, port
 507  	}
 508  	h := addr[:i]
 509  	ps := addr[i+1:]
 510  	if h != "" {
 511  		octets := bytes.Split([]byte(h), []byte("."))
 512  		for j := 0; j < 4 && j < len(octets); j++ {
 513  			v := 0
 514  			for k := 0; k < len(octets[j]); k++ {
 515  				ch := octets[j][k]
 516  				if ch >= '0' && ch <= '9' {
 517  					v = v*10 + int32(ch-'0')
 518  				}
 519  			}
 520  			ip[j] = byte(v)
 521  		}
 522  	}
 523  	pv := 0
 524  	for k := 0; k < len(ps); k++ {
 525  		ch := ps[k]
 526  		if ch >= '0' && ch <= '9' {
 527  			pv = pv*10 + int32(ch-'0')
 528  		}
 529  	}
 530  	return ip, int32(pv)
 531  }
 532  
 533  func main() {
 534  	c := newConfig()
 535  	listenAddr := "127.0.0.1:3100"
 536  
 537  	for i := 1; i < len(os.Args); i++ {
 538  		switch os.Args[i] {
 539  		case "-repos":
 540  			i++
 541  			if i < len(os.Args) {
 542  				c.setRoot(os.Args[i])
 543  			}
 544  		case "-listen":
 545  			i++
 546  			if i < len(os.Args) {
 547  				listenAddr = os.Args[i]
 548  			}
 549  		case "-host":
 550  			i++
 551  			if i < len(os.Args) {
 552  				c.setHost(os.Args[i])
 553  			}
 554  		}
 555  	}
 556  
 557  	ip, port := parseListen(listenAddr)
 558  
 559  	fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
 560  	if err != nil {
 561  		fmt.Fprintf(os.Stderr, "socket: %v\n", err)
 562  		os.Exit(1)
 563  	}
 564  	syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
 565  
 566  	sa := &syscall.SockaddrInet4{Port: int32(port), Addr: ip}
 567  	if berr := syscall.Bind(fd, sa); berr != nil {
 568  		fmt.Fprintf(os.Stderr, "bind: %v\n", berr)
 569  		os.Exit(1)
 570  	}
 571  	if lerr := syscall.Listen(fd, 128); lerr != nil {
 572  		fmt.Fprintf(os.Stderr, "listen: %v\n", lerr)
 573  		os.Exit(1)
 574  	}
 575  	fmt.Printf("git-lfs-http %s repos=%s host=%s\n", listenAddr, c.repoRoot, c.hostURL)
 576  
 577  	for {
 578  		nfd, _, aerr := syscall.Accept(fd)
 579  		if aerr != nil {
 580  			continue
 581  		}
 582  		req, rerr := readRequest(nfd, c.maxBatch)
 583  		if rerr != nil {
 584  			sendError(nfd, 400, rerr.Error())
 585  			syscall.Close(nfd)
 586  			continue
 587  		}
 588  		handler(nfd, c, req)
 589  		syscall.Close(nfd)
 590  	}
 591  }
 592