#!/usr/bin/python -tt # skvidal # gpl blah # code lifted from all over the place # complete and utter hackjob # I wanted a way to reconnect incoming git clone to a git:// server # to other servers on the backend w/o having to store the repo on disk # this is a dirty dirty dirty hackjob to do that. # I have no idea how reliable this is or not but it does seem to work import SocketServer import sys import socket import urlparse import select import time # PATHS TO STUFF # update this dict with: # path/on/server = git://url/to/real/repo # so if you ran: # git clone git://localhost/gitpod.git # you would receive a clone of git://github.com/sitaramc/gitpod.git proxydict = { '/gitpod.git':'git://github.com/sitaramc/gitpod.git', '/ansible/ansible.git':'git://github.com/ansible/ansible.git', } def opengit(giturl): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) url = urlparse.urlparse(giturl) sock.connect((url.netloc, 9418)) mystr = 'git-upload-pack %s\0host=%s\0' % (url.path, url.netloc) mylen = hex(len(mystr)).replace('0x','').zfill(4) sock.sendall('%s%s' % (mylen, mystr)) return sock class MyTCPHandler(SocketServer.BaseRequestHandler): """ The RequestHandler class for our server. It is instantiated once per connection to the server, and must override the handle() method to implement communication to the client. """ def handle(self): # self.request is the TCP socket connected to the client self.data = self.request.recv(4) len = int(self.data, 16) req = self.request.recv(len) req = req.split('\0')[0] repo = req.split()[1] print 'requested repo = %s' % repo # figure out the repo they want print 'real repo is: %s' % proxydict.get(repo, 'None') sock = opengit(proxydict.get(repo,'NONE')) done = False while not done: time.sleep(0.0001) inputready, outputready, exceptready = select.select([self.request, sock], [], []) for s in inputready: if s == self.request: other = sock data = s.recv(4096) else: other = self.request data = sock.recv(4096) if not data: done = True other.send(data) print 'closing' sock.close() if __name__ == "__main__": HOST, PORT = "0.0.0.0", 9418 server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) # Activate the server; this will keep running until you # interrupt the program with Ctrl-C server.serve_forever()