Repository navigation
Expand file tree
/
Copy pathhttps_server.py
More file actions
721 lines (581 loc) · 23 KB
/
Copy pathhttps_server.py
File metadata and controls
721 lines (581 loc) · 23 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
"""
server
"""
import socket
import ssl
import os
from urllib.parse import unquote,unquote_plus
import mimetypes
import markdown
import re
from html import escape
from urllib.parse import parse_qs
# Define socket host and port
SERVER_HOST = '0.0.0.0'
SERVER_PORT = 8443
VAULT_DIR = "C:/Users/Viren/ObsdianVaults/obsvault/obs.vault"
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain("cert.pem","key.pem")#public cert, private key
# Create socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((SERVER_HOST, SERVER_PORT))
server_socket.listen(2)#no. of clients
print('Listening on port %s ...' % SERVER_PORT)
secure_server = context.wrap_socket(server_socket,server_side=True)
def safe_path(base, user_path):
full_path = os.path.normpath(os.path.join(base, user_path))
base_abs = os.path.abspath(base)
full_abs = os.path.abspath(full_path)
if not full_abs.startswith(base_abs):
return None
return full_abs
def resolve_link(filename,current_dir):
candidate = os.path.join(current_dir,filename)
candidate_md = candidate + ".md"
full_path = os.path.join(VAULT_DIR,candidate_md)
if os.path.exists(full_path):
return candidate_md.replace("\\","/")
#fallback
for root,dirs,files in os.walk(VAULT_DIR):
for f in files:
if f == filename or f.startswith(filename + ".md"):
rel_path = os.path.relpath(os.path.join(root,filename),VAULT_DIR)
return rel_path.replace("\\","/")
return None
def parse_obsidian_embeds(md_text):
def repl(match):
filename = match.group(1)
# 👇 only treat images
if filename.lower().endswith((".png", ".jpg", ".jpeg", ".webp", ".gif")):
path = f"Assets Tags Templates/Assets/{filename}"
return f'<img src="/file/{path}">'
# 👇 if not image, leave unchanged
return match.group(0)
return re.sub(r'!\[\[([^\]]+)\]\]', repl, md_text)
def parse_obsidian_links(md_text,current_dir):
def repl(match):
filename = match.group(1)
# path = filename # keep simple for now
# print(path)
path = resolve_link(filename, current_dir)
if path:
return f'<a href="/file/{path}">{filename}</a>'
else:
return filename
return re.sub(r'(?<!\!)\[\[([^\]]+)\]\]', repl, md_text)
def render_folder(subpath=""):
full_path = safe_path(VAULT_DIR, subpath)
if not full_path or not os.path.exists(full_path):
return "<h1>Access Denied</h1>"
title = subpath if subpath else "Index"
try:
items = os.listdir(full_path)
except Exception:
return "<h1>Error reading directory</h1>"
has_subdirs = any(os.path.isdir(os.path.join(full_path, i)) for i in items)
html = f"""
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {{
font-family: sans-serif;
font-size: 20px;
}}
a {{
text-decoration: none;
}}
li {{
margin: 8px 0;
}}
.new-file-form {{
margin-top: 30px;
padding-top: 20px;
border-top: 1px solid #ccc;
}}
.new-file-form input[type="text"] {{
font-size: 18px;
padding: 6px;
width: 300px;
}}
.new-file-form button {{
font-size: 18px;
padding: 6px 14px;
margin-left: 8px;
}}
</style>
</head>
<body>
<h1>{title}</h1>
<ul>
"""
for item in items:
item_path = os.path.join(full_path, item)
new_path = os.path.join(subpath, item).replace("\\", "/")
if os.path.isdir(item_path):
html += f'<li><a href="/browse/{new_path}">{item}</a></li>'
else:
html += f'<li><a href="/file/{new_path}">{item}</a></li>'
html += "</ul>"
# for leaf folders, show new file form
if not has_subdirs:
html += f"""
<div class="new-file-form">
<form method="POST" action="/newfile">
<input type="text" name="filename" placeholder="new-note.md" required>
<input type="hidden" name="folder" value="{subpath}">
<button type="submit">+ New File</button>
</form>
</div>
"""
html += "</body></html>"
return html
#main loop
while True:
# Wait for client connections
#client_connection, client_address = server_socket.accept()
try:
client_connection, client_address = secure_server.accept()#tls handshake here
print(f"client connected: {client_address}")
# Get the client request
request = client_connection.recv(8192)
req_str = request.decode('utf-8',errors='ignore')
print(req_str)
#parse headers
headers = req_str.split('\n')
if len(headers) == 0 or len(headers[0].split()) < 2:
client_connection.close()
continue
method = headers[0].split()[0]
filename = headers[0].split()[1]
#Routes
#/
if method == "GET" and (filename == "/" or filename == "/browse/" or filename == "/browse"):
html = render_folder("")
response = (
b"HTTP/1.0 200 OK\r\n"
b"Content-Type: text/html\r\n"
b"\r\n" +
html.encode()
)
client_connection.sendall(response)
client_connection.close()
continue
#browse
elif method == "GET" and filename.startswith("/browse/"):
folder = filename[len("/browse/"):]
folder = unquote(folder)
html = render_folder(folder)
response=(
b"HTTP/1.0 200 OK\r\n"
b"Content-Type: text/html\r\n"
b"\r\n" +
html.encode()
)
client_connection.sendall(response)
client_connection.close()
continue
#files
elif method == "GET" and filename.startswith("/file/"):
file_path = unquote(filename[len("/file/"):])
full_path = safe_path(VAULT_DIR, file_path)
print(file_path)
print(full_path)
if not full_path or not os.path.exists(full_path):
response = (
b"HTTP/1.0 404 Not Found\r\n"
b"Content-Type: text/html\r\n"
b"\r\n"
b"<h1>404 Not Found</h1>"
)
client_connection.sendall(response)
client_connection.close()
continue
try:
with open(full_path, "rb") as f:
content = f.read()
#markdown handle
if full_path.endswith(".md"):
current_dir = os.path.dirname(file_path)
md_text = content.decode(errors="ignore")
md_text = parse_obsidian_embeds(md_text)
md_text = parse_obsidian_links(md_text,current_dir)
html_body = markdown.markdown(md_text)
#html_body = markdown.markdown(content.decode(errors="ignore"))
# html_body = html_body.replace('src="', 'src="/file/')
# def Write():
# print("hello")
# #get the file, have an input box in the html at the end
# # have an button that takes all the characters in the input box and does a post.
# # i think i have to first just break/ continue and give the filename
# # saveObj = {
# # "html_Body" : html_body,
# # }
# full_path +="/save"
# continue
html = f"""
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {{
font-family: sans-serif;
max-width: 800px;
margin: auto;
padding: 20px;
line-height: 1.6;
}}
img {{
max-width: 100%;
}}
</style>
</head>
<body>
{html_body}
<a href="/save/{file_path}">Edit</a>
</body>
</html>
"""
response = (
b"HTTP/1.0 200 OK\r\n"
b"Content-Type: text/html\r\n"
b"\r\n" +
html.encode()
)
client_connection.sendall(response)
client_connection.close()
continue
#mimetype code
else:
# MIME type detection
mime_type, _ = mimetypes.guess_type(full_path)
if mime_type is None:
mime_type = "application/octet-stream"
response = (
b"HTTP/1.0 200 OK\r\n" +
f"Content-Type: {mime_type}\r\n".encode() +
b"\r\n" +
content
)
client_connection.sendall(response)
client_connection.close()
continue
except Exception:
response = (
b"HTTP/1.0 500 Internal Server Error\r\n"
b"Content-Type: text/html\r\n"
b"\r\n"
b"<h1>Error reading file</h1>"
)
client_connection.sendall(response)
client_connection.close()
continue
# /save
#save
elif method == "GET" and filename.startswith("/save/"):
print("hello")
file_path = filename[len("/save/"):]
file_path = unquote(file_path)
print("GET PATH:", repr(file_path))
full_path = safe_path(VAULT_DIR, file_path)
if not full_path or not os.path.exists(full_path):
response = (
b"HTTP/1.0 404 Not Found\r\n"
b"Content-Type: text/html\r\n"
b"\r\n"
b"<h1>404 Not Found</h1>"
)
client_connection.sendall(response)
client_connection.close()
continue
try:
with open(full_path, "rb") as f:
content = f.read()
if full_path.endswith(".md"):
current_dir = os.path.dirname(file_path)
md_text = content.decode(errors="ignore")
md_text = parse_obsidian_embeds(md_text)
md_text = parse_obsidian_links(md_text, current_dir)
html_body = markdown.markdown(md_text)
html = f"""
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {{
font-family: sans-serif;
max-width: 800px;
margin: auto;
padding: 20px;
line-height: 1.6;
}}
img {{
max-width: 100%;
}}
textarea {{
width: 100%;
height: 200px;
margin-top: 20px;
}}
</style>
<script>
txt = document.getElememtById("journal-box")
</script>
</head>
<body>
{html_body}
<form method="POST" action="/save">
<textarea name="content"></textarea>
<input type="hidden" name="path" value="{file_path}">
<button type="submit">Append</button>
<a href="/overwrite/{file_path}">Overwrite</a>
</form>
</body>
</html>
"""
response = (
b"HTTP/1.0 200 OK\r\n"
b"Content-Type: text/html\r\n"
b"\r\n" +
html.encode()
)
client_connection.sendall(response)
client_connection.close()
continue
except Exception:
response = (
b"HTTP/1.0 500 Internal Server Error\r\n"
b"Content-Type: text/html\r\n"
b"\r\n"
b"<h1>Error reading file</h1>"
)
client_connection.sendall(response)
client_connection.close()
continue
elif method == "POST" and filename == "/save":
print("POST")
#request = client_connection.recv(8192)
headers_part, body = request.split(b"\r\n\r\n",1)
body_str = body.decode()
pairs = body_str.split("&")
data = {}
for pair in pairs:
if "=" in pair:
key,value = pair.split("=",1)
data[key] = unquote_plus(value)
content = data.get("content","").strip()
path = data.get("path","")
print("POST PATH:", repr(path))
absPath = safe_path(VAULT_DIR,path)
if not absPath or not os.path.exists(absPath):
response =(
b"HTTP/1.0 500 Internal Server Error\r\n"
b"Content-Type: text/html\r\n"
b"\r\n"
b"<h1>Error File Not Found</h1>"
)
client_connection.sendall(response)
client_connection.close()
continue
content = content + "\n"
with open(absPath,'a',encoding="utf-8") as myFile:
myFile.write("\n"+content)
response = (
b"HTTP/1.0 200 OK\r\n"
b"Content-Type: text/html\r\n"
b"\r\n"
b"<h1>Saved</h1>"
)
client_connection.sendall(response)
client_connection.close()
continue
elif method == "GET" and filename.startswith("/overwrite/"):
print("HIT OVERWRITE GET")
file_path = filename[len("/overwrite/"):]
file_path = unquote(file_path)
full_path = safe_path(VAULT_DIR, file_path)
if not full_path or not os.path.exists(full_path):
response = (
b"HTTP/1.0 404 Not Found\r\n"
b"Content-Type: text/html\r\n\r\n"
b"<h1>404 Not Found</h1>"
)
client_connection.sendall(response)
client_connection.close()
continue
import html
with open(full_path, "r", encoding="utf-8") as f:
content = f.read()
safe_content = html.escape(content)
html_page = f"""
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {{
margin: 0;
}}
button {{
display: block;
margin: 10px auto;
padding: 8px 20px;
font-size: 16px;
}}
textarea {{
display: block;
width: 80%;
height: 65vh;
box-sizing: border-box;
padding: 15px;
font-size: 16px;
font-family: monospace;
margin: 0 auto;
}}
</style>
</head>
<body>
<h2>Editing: {file_path}</h2>
<form method="POST" action="/overwrite">
<textarea name="content">{safe_content}</textarea>
<input type="hidden" name="path" value="{file_path}">
<br>
<button type="submit">Save</button>
</form>
</body>
</html>
"""
response = (
b"HTTP/1.0 200 OK\r\n"
b"Content-Type: text/html\r\n\r\n" +
html_page.encode()
)
client_connection.sendall(response)
client_connection.close()
continue
elif method == "POST" and filename == "/overwrite":
parts = request.split(b"\r\n\r\n", 1)
if len(parts) < 2:
client_connection.close()
continue
headers_part = parts[0].decode(errors='ignore')
body = parts[1]
content_length = 0
for line in headers_part.split('\r\n'):
if line.lower().startswith("content-length:"):
content_length = int(line.split(":", 1)[1].strip())
break
while len(body) < content_length:
chunk = client_connection.recv(8192)
if not chunk:
break
body += chunk
data = parse_qs(body.decode('utf-8', errors='ignore'))
content = data.get("content", [""])[0]
path = data.get("path", [""])[0]
if not path:
print("Error: No path received in POST")
client_connection.close()
continue
absPath = safe_path(VAULT_DIR, path)
print("Targeting file:", absPath)
if absPath and os.path.exists(absPath) and os.path.isfile(absPath):
with open(absPath, 'w', encoding="utf-8") as myFile:
myFile.write(content)
response = (
b"HTTP/1.0 200 OK\r\n"
b"Content-Type: text/html\r\n\r\n"
b"<h1>Saved</h1><a href='/file/" + path.encode() + b"'>Back</a>"
)
else:
print("Permission Error Prevention: Path is directory or invalid.")
response = b"HTTP/1.0 403 Forbidden\r\n\r\n<h1>Cannot overwrite a directory</h1>"
client_connection.sendall(response)
client_connection.close()
continue
elif method == "POST" and filename == "/newfile":
parts = request.split(b"\r\n\r\n", 1)
if len(parts) < 2:
client_connection.close()
continue
headers_part = parts[0].decode(errors='ignore')
body = parts[1]
content_length = 0
for line in headers_part.split('\r\n'):
if line.lower().startswith("content-length:"):
content_length = int(line.split(":", 1)[1].strip())
break
while len(body) < content_length:
chunk = client_connection.recv(8192)
if not chunk:
break
body += chunk
data = parse_qs(body.decode('utf-8', errors='ignore'))
filename_new = data.get("filename", [""])[0].strip()
folder = data.get("folder", [""])[0].strip()
# Add .md extension if user forgot it
if filename_new and not os.path.splitext(filename_new)[1]:
filename_new += ".md"
if not filename_new:
response = (
b"HTTP/1.0 400 Bad Request\r\n"
b"Content-Type: text/html\r\n\r\n"
b"<h1>No filename provided</h1>"
)
client_connection.sendall(response)
client_connection.close()
continue
new_rel_path = os.path.join(folder, filename_new).replace("\\", "/")
new_abs_path = safe_path(VAULT_DIR, new_rel_path)
if not new_abs_path:
response = (
b"HTTP/1.0 403 Forbidden\r\n"
b"Content-Type: text/html\r\n\r\n"
b"<h1>Invalid path</h1>"
)
client_connection.sendall(response)
client_connection.close()
continue
if os.path.exists(new_abs_path):
response = (
b"HTTP/1.0 409 Conflict\r\n"
b"Content-Type: text/html\r\n\r\n"
b"<h1>File already exists</h1>"
)
client_connection.sendall(response)
client_connection.close()
continue
# Create the empty file
with open(new_abs_path, 'w', encoding='utf-8') as f:
f.write("")
# Redirect to the overwrite/edit page so user can start writing
redirect_path = new_rel_path.replace(" ", "%20")
response = (
b"HTTP/1.0 302 Found\r\n"
b"Location: /overwrite/" + redirect_path.encode() + b"\r\n"
b"\r\n"
)
client_connection.sendall(response)
client_connection.close()
continue
# 🔹 Unknown route
else:
response = (
b"HTTP/1.0 404 Not Found\r\n"
b"Content-Type: text/html\r\n"
b"\r\n"
b"<h1>Route Not Found</h1>"
)
client_connection.sendall(response)
client_connection.close()
except ssl.SSLError as e:
print("Handshake failed:", e)
continue
except Exception as e:
print("Other error:", e)
continue
secure_server.close()