This repository has been archived by the owner on Mar 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
servefiles.py
86 lines (67 loc) · 1.99 KB
/
servefiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
import os
import socket
import struct
import sys
import threading
import time
import urllib
try:
from SimpleHTTPServer import SimpleHTTPRequestHandler
from SocketServer import TCPServer
from urlparse import urljoin
from urllib import pathname2url, quote
except ImportError:
from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer
from urllib.parse import urljoin, quote
from urllib.request import pathname2url
if len(sys.argv) < 3:
print("Usage: " + sys.argv[0] + " <ip> <file/directory> [host ip]")
sys.exit(1)
ip = sys.argv[1]
directory = sys.argv[2]
if not os.path.exists(directory):
print(directory + ": No such file or directory.")
sys.exit(1)
if len(sys.argv) >= 4:
hostIp = sys.argv[3]
else:
hostIp = [(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]
print("Preparing data...")
baseUrl = hostIp + ":8080/"
payload = ""
if os.path.isfile(directory):
payload += baseUrl + quote(os.path.basename(directory))
directory = os.path.dirname(directory)
else:
for file in [ file for file in next(os.walk(directory))[2] if file.endswith(('.cia', '.tik')) ]:
payload += baseUrl + quote(file) + "\n"
if len(payload) == 0:
print("No files to serve.")
sys.exit(1)
payloadBytes = payload.encode("ascii")
if not directory == "":
os.chdir(directory)
print("")
print("URLS:")
print(payload)
print("")
print("Opening HTTP server on port 8080...")
server = TCPServer(("", 8080), SimpleHTTPRequestHandler)
thread = threading.Thread(target=server.serve_forever)
thread.start()
try:
print("Sending URL(s) to " + ip + ":5000...")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, 5000))
sock.sendall(struct.pack('!L', len(payloadBytes)) + payloadBytes)
while len(sock.recv(1)) < 1:
time.sleep(0.05)
sock.close()
except Exception as e:
print("Error: " + str(e))
server.shutdown()
sys.exit(1)
print("Shutting down HTTP server...")
server.shutdown()