| |
| /pliant/util/crypto/proxy.pli |
| |
| 1 |
module "/pliant/language/unsafe.pli" | |
| 2 |
module "/pliant/language/stream.pli" | |
| 3 |
module "channel.pli" | |
| 4 |
module "/pliant/fullpliant/user.pli" | |
| 5 |
module "/pliant/protocol/http/site.pli" | |
| 6 |
module "/pliant/protocol/dns/name.pli" | |
| 7 |
module "/pliant/admin/md5.pli" | |
| 8 |
module "/pliant/util/crypto/cipher.pli" | |
| 9 |
module "/pliant/language/type/text/str8.pli" | |
| 10 |
| |
| 11 |
module "/pliant/protocol/dns/client.pli" # force using Pliant names database | |
| 12 |
| |
| 13 |
constant debug false | |
| 14 |
constant trace true | |
| 15 |
constant secured_proxy_tcp_port 9999 | |
| 16 |
| |
| 17 |
if debug | |
| 18 |
gvar Int counter := 0 | |
| 19 |
gvar FastSem sem | |
| 20 |
| |
| 21 |
| |
| 22 |
function proxy src dest public stop | |
| 23 |
arg Str src dest ; arg CBool public ; arg_rw CBool stop | |
| 24 |
stop := false | |
| 25 |
while not stop | |
| 26 |
part session | |
| 27 |
var Link:Stream s :> new Stream | |
| 28 |
s open src in+out+noautopost+safe | |
| 29 |
if not public and (s query "remote_ip_address")<>"127.0.0.1" | |
| 30 |
console "Somebody at "+(s query "remote_ip_address")+" is trying to use your proxy !!!" eol | |
| 31 |
leave session | |
| 32 |
thread | |
| 33 |
var Link:Stream d :> new Stream | |
| 34 |
d open dest in+out+noautopost+safe | |
| 35 |
if trace | |
| 36 |
if d=success | |
| 37 |
console "." | |
| 38 |
else | |
| 39 |
console "connection failed" eol | |
| 40 |
var FileInfo info := file_query dest extended | |
| 41 |
var Str key := info:options option "client_key" Str | |
| 42 |
if key<>"" | |
| 43 |
console "client key is " key eol | |
| 44 |
var Str key := info:options option "server_key" Str | |
| 45 |
if key<>"" | |
| 46 |
console "server key is " key eol | |
| 47 |
if debug | |
| 48 |
sem request ; counter += 2 ; console counter eol ; sem release | |
| 49 |
thread | |
| 50 |
while { d read_available (var Address adr2) (var Int size2) ; size2<>0 } | |
| 51 |
if debug | |
| 52 |
console " <-" size2 | |
| 53 |
s raw_write adr2 size2 | |
| 54 |
s flush anytime | |
| 55 |
if debug | |
| 56 |
console ";" | |
| 57 |
s safe_configure "shutdown" | |
| 58 |
if debug | |
| 59 |
sem request ; counter -= 1 ; console counter " " d:name eol ; sem release | |
| 60 |
while { s read_available (var Address adr1) (var Int size1) ; size1<>0 } | |
| 61 |
if debug | |
| 62 |
console " ->" size1 | |
| 63 |
d raw_write adr1 size1 | |
| 64 |
d flush anytime | |
| 65 |
if debug | |
| 66 |
console ";" | |
| 67 |
d safe_configure "shutdown" | |
| 68 |
if debug | |
| 69 |
sem request ; counter -= 1 ; console counter " " s:name eol ; sem release | |
| 70 |
| |
| 71 |
| |
| 72 |
gvar Str proxy_current_site | |
| 73 |
gvar CBool stop | |
| 74 |
gvar Sem sem | |
| 75 |
| |
| 76 |
function proxy user password site -> status | |
| 77 |
arg Str user password site ; arg ExtendedStatus status | |
| 78 |
stop := true | |
| 79 |
(var Stream s) open "tcp://127.0.0.1/client/"+string:secured_proxy_tcp_port in+out+safe | |
| 80 |
s close | |
| 81 |
if not (site parse any:(var Str host) ":" (var Int port)) | |
| 82 |
host := site | |
| 83 |
var Data:NameHost h :> name_database:data:host site | |
| 84 |
if exists:h | |
| 85 |
port := h http_port | |
| 86 |
else | |
| 87 |
var Data:Site si :> site_database:data:site site | |
| 88 |
if exists:si and si:port=defined | |
| 89 |
port := si port | |
| 90 |
eif exists:si and si:computer<>"" and exists:(name_database:data:host si:computer) | |
| 91 |
port := (name_database:data:host si:computer) http_port | |
| 92 |
else | |
| 93 |
port := 80 | |
| 94 |
proxy_current_site := "" | |
| 95 |
if site="" | |
| 96 |
return success | |
| 97 |
sem request | |
| 98 |
var CBool ok := user_secret_database:data:user:user:key_md5="" or user_secret_database:data:user:user:key_md5=string_md5_hexa_signature:(uncipher (straight_to_Str user_secret_database:data:user:user:private_key) password) | |
| 99 |
if not ok | |
| 100 |
sleep 2 | |
| 101 |
sem release | |
| 102 |
return (failure "Password for '"+user+"' is wrong.") | |
| 103 |
sem release | |
| 104 |
proxy_current_site := host | |
| 105 |
thread | |
| 106 |
proxy "tcp:/server/"+string:secured_proxy_tcp_port "zchannel://"+host+"/client/"+(string port+500)+"/"+host+"/"+user+"/"+password false stop | |
| 107 |
status := success | |
| 108 |
| |
| 109 |
export proxy proxy_current_site secured_proxy_tcp_port | |
| |