Skip to content

Commit

Permalink
add reverse-proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
9001 committed Nov 17, 2020
1 parent 755a2ee commit 1aa1b34
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
4 changes: 4 additions & 0 deletions contrib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
init-scripts to start copyparty as a service
* [`systemd/copyparty.service`](systemd/copyparty.service)
* [`openrc/copyparty`](openrc/copyparty)

# Reverse-proxy
copyparty has basic support for running behind another webserver
* [`nginx/copyparty.conf`](nginx/copyparty.conf)
26 changes: 26 additions & 0 deletions contrib/nginx/copyparty.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
upstream cpp {
server 127.0.0.1:3923;
keepalive 120;
}
server {
listen 443 ssl;
listen [::]:443 ssl;

server_name fs.example.com;

location / {
proxy_pass http://cpp;
proxy_redirect off;
# disable buffering (next 4 lines)
proxy_http_version 1.1;
client_max_body_size 0;
proxy_buffering off;
proxy_request_buffering off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "Keep-Alive";
}
}
6 changes: 5 additions & 1 deletion copyparty/httpcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def run(self):
v = self.headers.get("connection", "").lower()
self.keepalive = not v.startswith("close")

v = self.headers.get("x-forwarded-for", None)
if v is not None and self.conn.addr[0] in ["127.0.0.1", "::1"]:
self.log_src = self.conn.set_rproxy(v.split(",")[0])

self.uname = "*"
if "cookie" in self.headers:
cookies = self.headers["cookie"].split(";")
Expand Down Expand Up @@ -462,7 +466,7 @@ def handle_post_binary(self):

spd = self._spd(post_sz)
self.log("{} thank".format(spd))
self.reply("thank")
self.reply(b"thank")
return True

def handle_login(self):
Expand Down
14 changes: 13 additions & 1 deletion copyparty/httpconn.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, sck, addr, hsrv):
self.nbyte = 0
self.workload = 0
self.log_func = hsrv.log
self.log_src = "{} \033[36m{}".format(addr[0], addr[1]).ljust(26)
self.set_rproxy()

env = jinja2.Environment()
env.loader = jinja2.FileSystemLoader(os.path.join(E.mod, "web"))
Expand All @@ -56,6 +56,18 @@ def __init__(self, sck, addr, hsrv):
self.tpl_md = env.get_template("md.html")
self.tpl_mde = env.get_template("mde.html")

def set_rproxy(self, ip=None):
if ip is None:
color = 36
ip = self.addr[0]
self.rproxy = None
else:
color = 34
self.rproxy = ip

self.log_src = "{} \033[{}m{}".format(ip, color, self.addr[1]).ljust(26)
return self.log_src

def respath(self, res_name):
return os.path.join(E.mod, "web", res_name)

Expand Down

0 comments on commit 1aa1b34

Please sign in to comment.