Proxy Server for mIRC — File Browser

ProxyServerformIRCv3.4 / ProxyServerformIRCv3.4 / proxy.mrc

proxy.mrc — 49.67 KB — Download this file

; Proxy Server for mirc Made by Tontito (tontitoscript@gmail.com)
; this allows you to connect to internet using another pc
; the pc that is connected to the internet must run this script
; the pc client must have is browser with proxy settings IP and port 8080 or any other
; version 3.4
; Addon released to www.mircscripts.org


Menu Menubar,status {
  -
  Proxy Server
  .Commands
  ..Listen:escuta
  ..-
  ..Stop:para
  ..-
  ..Set parental pass: {
    var %tmp
    if ($readf(parental_pass)) {
      %tmp = $$?*="Please type your access password"
      if ($md5(:Tontito Proxy Server Authenticate System $+ : $+ %tmp) != $readf(parental_pass)) return
    }

    %tmp = $$?*="Please type your new access password"
    %tmp = $remove(%tmp,=)
    $writef(parental_pass,$md5(:Tontito Proxy Server Authenticate System $+ : $+ %tmp))
  }
  .-
  .Config menu:conf
  .-
  .View Logs: run $shortfn($scriptdir) $+ Logs
  .-
  .Checkupdate:checkupdate
  .-
  .Help:help
}


on 1:start: {
  var %file = $shortfn($scriptdir) $+ configs\Hosts.ric
  if ($exists(%file)) {
    if (!$hget(proxy_shosts)) { hmake proxy_shosts 20 }
    hload proxy_shosts %file
  }

  %file = $shortfn($scriptdir) $+ configs\Routes.ric
  if ($exists(%file)) {
    if (!$hget(proxy_routes)) { hmake proxy_routes 20 }
    hload proxy_routes %file
  }

  %file = $shortfn($scriptdir) $+ configs\Extensions.ric
  if ($exists(%file)) {
    if (!$hget(proxy_ext)) { hmake proxy_ext 20 }
    hload proxy_ext %file
  }

  if ($readf(proxy_autostart)) { checkupdate | escuta }
}


on 1:load:{
  if ($version < 6.35) {
    echo 4 -a * This mirc version is $version ... Please use at least 6.35.
    .unload -rs $shortfn($script) | halt
  }

  mkdir $shortfn($scriptdir) $+ configs

  if (!$readf(proxy_http_port)) {
    echo 3 -a Please setup the rest of the Proxy server Options!
    conf
  }
  else {
    echo 3 -a Proxy ready to be used, just start the service!
  }
}

on 1:unload:{
  unset %proxy_*
  if ($sock(proxy)) sockclose proxy*
  if ($sock(telnet)) { sockclose telnet }

  if ($hget(cabecalhos)) hfree cabecalhos
  if ($hget(ip_cache)) hfree ip_cache
  if ($hget(proxy_shosts)) hfree proxy_shosts
  if ($hget(proxy_ext)) hfree proxy_ext
  if ($hget(proxy_routes)) hfree proxy_routes

  .timerproxy* off

  if ($readf(lan_ip) == 127.0.0.1) .firewall -c off
  echo 3 -a Addon unloaded, socks closed and all vars cleaned... bye.
}


alias -l escuta {
  if ($sock(proxy) || $sock(telnet)) { echo 3 -a Server already running! | return }

  if (!$exists($shortfn($scriptdir) $+ configs\proxy.ini)) {
    config | return
  }
  if (!$portfree($readF(proxy_http_port)) && ($readf(proxy_http) == yes)) { echo 4 -a There is already a program using port $readF(proxy_http_port) | return }
  if (!$portfree($readF(proxy_socks5_port)) && ($readf(proxy_socks5) == yes)) { echo 4 -a There is already a program using port $readF(proxy_socks5_port) | return }

  if (($readf(proxy_http) == yes) && ($readf(proxy_http_port))) socklisten -p proxy $readF(proxy_http_port)
  if (($readf(proxy_socks5) == yes) && ($readf(proxy_socks5_port))) socklisten telnet $readF(proxy_socks5_port)

  echo 3 -a Your proxy Web server is now running, you can now connect to the internet with this pc or
  echo 3 -a another pc in the same network(Lan) configed in config dialog (depending on the mask).
  echo 3 -a Available ports are:
  if ($readf(proxy_http_port) && $readf(proxy_http) && $sock(proxy)) { echo 3 -a $readF(proxy_http_port) on http and https }
  if ($readf(proxy_socks5_port) && $readf(proxy_socks5) && $sock(telnet)) { echo 3 -a and $readF(proxy_socks5_port) on socks5! }

  if ($hget(proxy_routes,0).item > 0) {
    echo 3 -a Starting routing services
    var %i = 1   
    while (%i <= $hget(proxy_routes,0).item) {
      echo $hget(proxy_routes,%i).item on port $gettok($hget(proxy_routes,%i).data,1,58)
      socklisten route_ $+ $hget(proxy_routes,%i).item $gettok($hget(proxy_routes,%i).data,1,58)
      inc %i
    }
    echo 3 -a Finished starting routing services
  }

  if (!$hget(cabecalhos)) { hmake cabecalhos 300 }
  var %file = $shortfn($scriptdir) $+ configs\ip_cache.ric
  if ($exists(%file)) {
    if (!$hget(ip_cache)) { hmake ip_cache 50 }
    hload ip_cache %file
  }
  mkdir $shortfn($scriptdir) $+ Logs
}


alias -l para {
  if (!$sock(proxy) && !$sock(telnet)) { echo 3 -a Proxy server not running! | return }
  sockclose proxy* | sockclose telnet 
  if ($hget(cabecalhos)) hfree cabecalhos 
  if ($hget(web_proxy_partial)) hfree web_proxy_partial
  if ($hget(ip_cache)) hfree ip_cache
  if ($hget(proxy_accgranted)) hfree proxy_accgranted

  if ($hget(proxy_routes,0).item > 0) {
    echo 3 -a Stopping routing services
    var %i = 1   
    while (%i <= $hget(proxy_routes,0).item) {
      echo $hget(proxy_routes,%i).item on port $gettok($hget(proxy_routes,%i).data,1,58)
      sockclose route_ $+ $hget(proxy_routes,%i).item $gettok($hget(proxy_routes,%i).data,1,58)
      inc %i
    }
    echo 3 -a Finished stopping routing services
  }

  echo 3 -a Proxy Server stopped!
}

alias -l help {
  echo 3 -a This server must run on the pc that is directly connected to the internet.
  echo 3 -a Works with all tested web programs on port $readF(proxy_http_port), any exception report it please!
  echo 3 -a and for socks5, works to outside of your lan for mirc and other based progs, and also allows recive connection (on my test cases).
  echo 3 -a Don't forget to setup the proxy of all web based programas to ip $readf(proxy_lip) and port $readf(proxy_http_port)
  echo 3 -a You must setup lan ip and mask correctly so the clients you expect can connect and the ones you don't want, don't connect.
  echo 3 -a Have a look on help file if you don't know how to setup the mask.
}

alias -l ret {
  if ($2) {
    if ($1 == 1) return $gettok($2,-1,95)
    else return $sock($2).mark
  }
  else {
    if ($1 == 1) return $gettok($sockname,-1,95)
    else return $sock($sockname).mark
  }
}

alias -l test_ip { 
  var %ip = $longip($sock($sockname).ip)
  if (((%ip <= $readf(begin_ip)) || (%ip >= $readf(end_ip))) && (127.0.0.1 != $sock($sockname).ip)) { echo 4 Not allowed access was blocked from $sock($sockname).ip | sockclose $sockname | halt }
}

alias -l writeF { .writeini $shortfn($scriptdir) $+ configs\proxy.ini options $1 $2 }
alias -l readF { return $readini $shortfn($scriptdir) $+ configs\proxy.ini options $1 }
alias -l remf { if ($1) { .remini $shortfn($scriptdir) $+ configs\proxy.ini options $1 } }


alias -l logger {
  var %sock = $sockname, %ip = $sock(%sock).ip

  var %logs = $shortfn($scriptdir) $+ Logs
  if (!$isdir(%logs)) mkdir %logs
  if (!$isdir(%logs $+ \Admin)) mkdir %logs $+ \Admin
  var %l_dir = %logs $+ \Admin\ $+ $replace($adate,/,-) $+ _USDateFormat $+ _Web_log.txt

  bwrite -t %l_dir -1 %sock --> $date $+ , $time $+ $crlf
  bcopy -c &var 1 $1 1 $calc($bfind($1,1,13 10 13 10) + 20)
  bwrite %l_dir -1 &var
}


;### WEB HANDLING

on 1:sockclose:proxy_web_l_*: {
  cleanups 
}

on 1:sockclose:proxy_web_c_*: {
  if ($sock(proxy_web_l_ $+ $ret(1)).mark = Opera) {
    ; echo 4 opera killing sockets
    ; cleanups 
  }

  if ($isalias(process_logs)) { process_logs $ret(1) }
}


on 1:sockread:proxy_web_l_*: le_web1
on 1:sockread:proxy_web_c_*: le_web2

on 1:sockwrite:proxy_web_c_*: $sendToServer($sockname)
on 1:sockwrite:proxy_web_l_*: $sendToClient($sockname)

on 1:socklisten:proxy: aceita_web
on 1:sockopen:proxy_web_c_*: connect_web


alias -l cleanups {
  var %sock = proxy_web_l_ $+ $ret(1)
  var %ret = $ret(1)

  sockclose proxy_web_c_ $+ %ret
  hdel -w cabecalhos %ret $+ *
  .timerproxy_ $+ %ret off 
  .timerproxy_+ $+ %ret off

  if (!%sock) return
  sockclose %sock

  if ($hget(web_proxy_partial)) hdel web_proxy_partial %sock
  if ($isalias(process_logs)) { process_logs %ret }
}


alias -l sendToServer {
  if (($sock($1).sq > 8192) && (!$timer(proxy_ $+ $ret(1,$1)))) { 
    ; .timerproxy_ $+ $ret(1,$1) -om 0 100 sendToServer $1 
    return 
  }

  var %i = $gettok($hget(cabecalhos,$ret(1,$1)),2,32)
  if (%i == $null) %i = 1

  var %lixo = $hget(cabecalhos,$ret(1,$1) $+ - $+ %i,&fich)
  if (%i && (%lixo > 0)) {
    %i = $gettok($hget(cabecalhos,$ret(1,$1)),2,32)

    while (%lixo > 0) {
      bunset &fich
      %lixo = $hget(cabecalhos,$ret(1,$1) $+ - $+ %i,&fich)
      if ((%lixo > 0) && ($sock($1).sq < 8192)) {
        hdel cabecalhos $ret(1,$1) $+ - $+ %i | sockwrite $1 &fich | bunset &fich | inc %i
      }
      else break
    }
    hadd cabecalhos $ret(1,$1) $gettok($hget(cabecalhos,$ret(1,$1)),1,32) %i
  }
}

alias -l sendToClient {
  if (($sock($1).sq > 8192) && (!$timer(proxy_ $+ $ret(1,$1)))) { .timerproxy_+ $+ $ret(1,$1) -om 0 20 sendToClient $1 | return }

  var %i = $gettok($hget(cabecalhos,$ret(1,$1) $+ +),2,32)
  if (%i == $null) %i = 999999
  inc %i
  var %lixo = $hget(cabecalhos,$ret(1,$1) $+ + $+ %i,&fich)

  if (%i && (%lixo > 0)) {
    %i = $gettok($hget(cabecalhos,$ret(1,$1) $+ +),2,32)
    if (%i == $null) %i = 999999
    inc %i

    while (%lixo > 0) {
      %lixo = $hget(cabecalhos,$ret(1,$1) $+ + $+ %i,&fich)

      if ((%lixo > 0) && ($sock($1).sq < 8192)) {
        hadd cabecalhos $ret(1,$1) $+ + $gettok($hget(cabecalhos,$ret(1,$1) $+ +),1,32) %i

        ;limpa em almost realtime
        hdel cabecalhos $ret(1,$1) $+ + $+ %i

        sockwrite $1 &fich | bunset &fich | inc %i
      }
      else { dec %i | break }
    }
    hadd cabecalhos $ret(1,$1) $+ + $gettok($hget(cabecalhos,$ret(1,$1) $+ +),1,32) %i
  }
  else if (%lixo = 0) {
    .timerproxy_+ $+ $ret(1,$1) off
  }
}


alias -l aceita_web {
  var %temp = $sock(proxy).mark
  if (!%temp || %temp > 65000) { %temp = 0 }

  :mais
  inc %temp
  if ($sock(proxy_web_l_ $+ %temp)) { goto mais }

  sockmark proxy %temp
  sockaccept proxy_web_l_ $+ %temp
}


alias -l connect_web {
  var %proxy = proxy_web_l_ $+ $ret(1)

  if (!$sock(%proxy)) { sockclose $sockname | return }

  if ($sockerr) {
    var %ip = $hfind(ip_cache,$sock($sockname).ip,1).data
    if (%ip) {
      hdel ip_cache %ip
      hsave -o ip_cache $shortfn($scriptdir) $+ configs\ip_cache.ric
    }
    cleanups 
    return
  }

  if (!%proxy_server) {
    var %pita = $hfind(ip_cache,$ret(1),1).data
    if (%pita) {
      hadd -m ip_cache %pita $sock($sockname).ip
      hsave -o ip_cache $shortfn($scriptdir) $+ configs\ip_cache.ric
    }
  }

  if (($ret(2) == https) && (!%proxy_server)) { sockwrite %proxy HTTP/1.1 200 Connection established $+ $crlf $+ Proxy-agent: Tontito Proxy Server $+ $crlf $+ $crlf }

  var %lixo = 1, %i = 1
  while (%lixo > 0) {
    bunset &fich
    %lixo = $hget(cabecalhos,$ret(1) $+ - $+ %i,&fich)

    if ((%lixo > 0) && ($sock($sockname).sq < 8192)) { hdel cabecalhos $ret(1) $+ - $+ %i | sockwrite $sockname &fich | bunset &fich | inc %i }
    else break
  }
  hadd -m cabecalhos $ret(1) $hget(cabecalhos,$ret(1)) %i
}


;### READING ALIAS
alias -l le_web1 {
  var %timpes = $ticks, %vari, %z, %host, %fullhost, %porta = 80, %i
  var %ligado = proxy_web_c_ $+ $ret(1)

  if ($sockerr > 0) {
    cleanups 
    return
  }

  sockread 5120 &lido
  if ($readf(proxy_logs)) bwrite $shortfn($scriptdir) $+ logs\web_ $+ $ret(1) $+ .log -1 &lido

  :reconnect

  if (!$sock(%ligado)) {
    if ($hget(web_proxy_partial,$sockname,&tmp)) {
      bcopy -c &tmp $calc($bvar(&tmp,0) + 1) &lido 1 -1
      hadd -mb web_proxy_partial $sockname &tmp
      bcopy -c &lido 1 &tmp 1 -1
      %i = $bfind(&lido, 1, 13 10 13 10)
      if (($bvar(&lido,0) > 10000) && ((!%i) || (%i > 10000))) { echo 4 Attack attempt on sock $sockname $+ ? | cleanups | return }
      if (!%i) return
    }
    else {
      %i = $bfind(&lido, 1, 13 10 13 10)
      if (($bvar(&lido,0) > 10000) && ((!%i) || (%i > 10000))) { echo 4 Attack attempt on sock $sockname $+ ? | cleanups | return }
      hadd -mb web_proxy_partial $sockname &lido
      if (!%i) return
    }

    ;clean cached request
    hdel web_proxy_partial $sockname

    ;block extensions/exploits
    if ($readf(filter_extensions) == on) {
      var %extension = $gettok($gettok($bget(&lido,http://,13 10),1,32),-1,46)
      if ($hget(proxy_ext,$mid(%extension,1,3))) { cleanups | return }
    }

    ;check ip range
    test_ip

    $logger(&lido)

    ;permissions
    if ($test_access(&lido) == 407) { $sendRequest() | return }

    $breplacer(&lido,Proxy-Connection:,13 10,Connection: Keep-Alive)
    $bremover(&lido,Proxy-Authorization:,13 10)

    if (Opera isin $bget(&lido,User-Agent:,13 10)) {
      sockmark $sockname Opera
    }

    %fullhost = $gettok($bget(&lido,Host:,13 10),2,32)
    %host = $gettok(%fullhost,1,58)
    if ($numtok(%fullhost,58) == 2) %porta = $gettok(%fullhost,2,58)

    var %aux, %lido, %dir
    %lido = $bvar(&lido,1,250).text
    if (CONNECT* iswm %lido) { https %lido }

    if (!%proxy_server) { $bconcat(&lido, $bnget(&lido,1,32), $getBvar(&lido,4,/), 32) }

    var %i = $gettok($hget(cabecalhos,$ret(1)),1,32)
    inc %i
    hadd -mb cabecalhos $ret(1) $+ - $+ %i &lido
    hadd cabecalhos $ret(1) %i $gettok($hget(cabecalhos,$ret(1)),2,32)


    ;### Cache of ips
    if (!%proxy_server) {
      var %ip = $hget(ip_cache,[ [ %host ] ])

      if (%ip && ($numtok(%ip,46) == 4)) { %host = %ip }
      else {
        hadd -m ip_cache %host $ret(1)
        hsave -o ip_cache $shortfn($scriptdir) $+ configs\ip_cache.ric
      }
    }

    if ((GET* iswm %lido) || (HEAD* iswm %lido)) {
      if (!%proxy_server) { sockopen %ligado %host %porta }
      else sockopen %ligado %proxy_server
    }
    elseif ((POST* iswm %lido) && $sock(%ligado)) {
      sockwrite %ligado &lido 
    }
    elseif (POST* iswm %lido) {
      if (!%proxy_server) { sockopen %ligado %host %porta }
      else sockopen %ligado %proxy_server
    }
    else {
      bwrite $shortfn($scriptdir) $+ Other_requests.txt -1 &lido
      cleanups 
    }

    ;### records the link
    sockmark proxy_web_c_ $+ $ret(1) $gettok(%lido,2,32)
  }
  elseif ($sock(%ligado).status == active) {
    if ($hget(web_proxy_partial,$sockname,&tmp)) {
      bcopy -c &tmp $calc($bvar(&tmp,0) + 1) &lido 1 -1
      hadd -mb web_proxy_partial $sockname &tmp
      bcopy -c &lido 1 &tmp 1 -1
      %i = $bfind(&lido, 1, 13 10 13 10)
      if (($bvar(&lido,0) > 10000) && ((!%i) || (%i > 10000))) { echo 5 Attack attempt on sock $sockname $+ ? | cleanups | return }
      if (!%i) return

      hdel web_proxy_partial $sockname

      %fullhost = $gettok($bget(&lido,Host:,13 10),2,32)
      %host = $gettok(%fullhost,1,58)

      if (($hget(ip_cache,%host) != $sock(%ligado).ip) && !%proxy_server) {
        ;não faz cleanups aqui senão fica tudo instavel
        hdel -w cabecalhos $ret(1) $+ *
        sockclose %ligado
        goto reconnect
        return
      }

      if (!%proxy_server) {
        $breplacer(&lido,Proxy-Connection:,13 10,Connection: Keep-Alive)
        $bremover(&lido,Proxy-Authorization:,13 10)
        var %replace = $bnget(&lido,1,13 10)
        $breplacer(&lido,%replace,13 10,$remove(%replace,http:// $+ %fullhost))
      }
    }
    else {
      %z = $bfind(&lido, 1, HTTP/1.)
      if (%z) {
        %i = $bfind(&lido, 1, 13 10 13 10)
        if (($bvar(&lido,0) > 10000) && ((!%i) || (%i > 10000))) { echo 5 Attack attempt on sock $+ ? | cleanups | return }
        hadd -mb web_proxy_partial $sockname &lido
        if (!%i) return

        %fullhost = $gettok($bget(&lido,Host:,13 10),2,32)
        %host = $gettok(%fullhost,1,58)

        hdel web_proxy_partial $sockname

        if (($hget(ip_cache,%host) != $sock(%ligado).ip) && !%proxy_server) {
          ;não faz cleanups aqui senão fica tudo instavel
          hdel -w cabecalhos $ret(1) $+ *
          sockclose %ligado
          goto reconnect
          return
        }

        if (!%proxy_server) {
          $breplacer(&lido,Proxy-Connection:,13 10,Connection: Keep-Alive)
          $bremover(&lido,Proxy-Authorization:,13 10)
          var %replace = $bnget(&lido,1,13 10)
          $breplacer(&lido,%replace,13 10,$remove(%replace,http:// $+ %fullhost))
        }
      }
    }

    var %i = $gettok($hget(cabecalhos,$ret(1)),1,32)
    var %j = $gettok($hget(cabecalhos,$ret(1)),2,32)
    if (%i == $null) %i = 1
    if (%j == $null) %j = 1

    if ((%i != %j) || ($sock(%ligado).sq > 8192)) {
      inc %i
      hadd -b cabecalhos $ret(1) $+ - $+ %i &lido
      hadd cabecalhos $ret(1) %i $gettok($hget(cabecalhos,$ret(1)),2,32)

      %i = $gettok($hget(cabecalhos,$ret(1)),2,32)
      var %lixo = 1

      while (%lixo > 0) {
        bunset &fich
        %lixo = $hget(cabecalhos,$ret(1) $+ - $+ %i,&fich)

        if ((%lixo > 0) && ($sock(%ligado).sq < 8192)) {
        hdel cabecalhos $ret(1) $+ - $+ %i | sockwrite %ligado &fich | bunset &fich | inc %i }
        else break
      }
      hadd cabecalhos $ret(1) $gettok($hget(cabecalhos,$ret(1)),1,32) %i
    }
    else {
      hdel web_proxy_partial $sockname
      sockwrite %ligado &lido
    }
  }
  elseif ($sock(%ligado).status != active) {
    if (!%proxy_server) {
      if ($hget(web_proxy_partial,$sockname,&tmp)) {
        bcopy -c &tmp $calc($bvar(&tmp,0) + 1) &lido 1 -1
        hadd -mb web_proxy_partial $sockname &tmp
        bcopy -c &lido 1 &tmp 1 -1
        %i = $bfind(&lido, 1, 13 10 13 10)
        if (($bvar(&lido,0) > 7000) && ((!%i) || (%i > 7000))) { echo 3 Attack attempt on sock $+ ? | sockclose $sockname | sockclose %ligado | return }
        if (!%i) return

        hdel web_proxy_partial $sockname

        $breplacer(&lido,Proxy-Connection:,13 10,Connection: Keep-Alive)
        $bremover(&lido,Proxy-Authorization:,13 10)
        var %replace = $bnget(&lido,1,13 10)
        $breplacer(&lido,%replace,13 10,$remove(%replace,http:// $+ $gettok($bget(&lido,Host:,13 10),2,32)))
      }
      else {
        %z = $bfind(&lido, 1, HTTP/1.)
        if (%z) {
          %i = $bfind(&lido, 1, 13 10 13 10)
          if (($bvar(&lido,0) > 7000) && ((!%i) || (%i > 7000))) { echo 3 Attack attempt on sock $+ ? | sockclose $sockname | sockclose %ligado | return }
          hadd -mb web_proxy_partial $sockname &lido
          if (!%i) return

          hdel web_proxy_partial $sockname

          $breplacer(&lido,Proxy-Connection:,13 10,Connection: Keep-Alive)
          $bremover(&lido,Proxy-Authorization:,13 10)
          var %replace = $bnget(&lido,1,13 10)
          $breplacer(&lido,%replace,13 10,$remove(%replace,http:// $+ $gettok($bget(&lido,Host:,13 10),2,32)))
        }
      }
    }

    var %i = $gettok($hget(cabecalhos,$ret(1)),1,32)
    inc %i
    hadd -b cabecalhos $ret(1) $+ - $+ %i &lido
    hadd cabecalhos $ret(1) %i $gettok($hget(cabecalhos,$ret(1)),2,32)
  }

  return

  :error
  echo There is an error logged in error.log with $error content, please send it to tontitoscript@gmail.com with subject (proxy server - bug report)
  write $shortfn($scriptdir) $+ error.log host = %host with port = %porta for socket $sockname with error $error
  if ($bvar(&lido,0)) bwrite $shortfn($scriptdir) $+ error.log -1 &lido
}


alias -l https {
  var %i, %host, %port, %lido = $gettok($gettok($1-,1,13),2,32)
  %host = $gettok(%lido,1,58)
  %port = $gettok(%lido,2,58)

  if (!%proxy_server) {
    var %ip = $hget(ip_cache,[ [ %host ] ])

    if (%ip && ($len(%ip) > 6)) { %host = %ip }
    else {
      hadd -m ip_cache %host $ret(1)
      hsave -o ip_cache $shortfn($scriptdir) $+ configs\ip_cache.ric
    }

    %i = $calc($bfind(&lido, 1, 13 10 13 10) + 4)

    if ($calc(%i - 1) < $bvar(&lido,0)) {
      bunset &fich
      bcopy &fich 1 &lido %i $bvar(&lido,0)
      hadd -b cabecalhos $ret(1) $+ - $+ 0 &fich
    }

    sockopen proxy_web_c_ $+ $ret(1) %host %port
  }
  else {
    hadd -b cabecalhos $ret(1) $+ - $+ 1 &lido
    sockopen proxy_web_c_ $+ $ret(1) %proxy_server
  }

  hadd -m cabecalhos $ret(1) 1 $gettok($hget(cabecalhos,$ret(1)),2,32)
  sockmark proxy_web_c_ $+ $ret(1) https
  halt
}


alias -l sendRequest {
  sockwrite -n $sockname HTTP/1.1 407 Unauthorized
  sockwrite -n $sockname Content-Type: text/html
  sockwrite -n $sockname Date: $asctime($gmt,ddd dd mmm yyyy HH:nn:ss) GMT
  sockwrite -n $sockname Server: Tontito's Mirc Proxy Server 2.25

  var %nonce = $md5($ticks $+ $rand(1,99999) $+ $gmt $+ $sock($sockname).ip)
  hadd -mu60 proxy_accgranted %nonce $sock($sockname).ip

  if ($readf(authdigest) == 1) sockwrite -n $sockname Proxy-Authenticate: Digest realm="Tontito Proxy Server Authenticate System", qop="auth", nonce=" $+ %nonce $+ ", opaque="5cce069c403ebaf9f0171e9517f40e41"
  else sockwrite -n $sockname Proxy-Authenticate: Basic realm="Proxy Server Authenticate System"

  sockwrite -n $sockname Connection: close
  sockwrite -n $sockname

  .timer $+ $sockname -m 1 500 sockclose $sockname
  hdel -w cabecalhos $ret(1) $+ *
  .timerproxy_ $+ $ret(1) off 
  .timerproxy_+ $+ $ret(1) off
}


;$writef(authdigest,1)

alias -l test_access {
  var %host = $gettok($gettok($bget(&lido,Host:,13 10),2,32),1,58)

  if ($hget(proxy_shosts,%host) == BL) {
    sockclose $sockname
    halt
  }

  var %proxy_aut = $gettok($bget(&lido,Proxy-Authorization: Basic,13 10),3,32)

  if (%proxy_aut) {
    if ($hget(proxy_shosts,%host) == AP) {
      if ($md5($gettok($decode($remove(%proxy_aut,=),m),1-,58)) != $readf(parental_pass)) { return 407 }
      else return ok
    }

    if (!$readf(proxy_pass)) { return ok }

    var %data = $decode($remove(%proxy_aut,=),m)
    %data = $gettok(%data,1,$asc(:)) $+ :Tontito Proxy Server Authenticate System: $+ $gettok(%data,2,$asc(:))

    if ($md5(%data) == $readf(proxy_pass) || $md5($gettok($decode(%data,m),1-,58)) == $readf(parental_pass)) { return ok }
    else { return 407 }
  }
  else {
    var %check = $replace($replace($bget(&lido,Proxy-Authorization: Digest,13 10), $chr(44) $+ $chr(32), $chr(44)),uri=",uri=)

    var %user = $gettok($wildtok(%check,*username="*,1,44),2,34)
    var %nonce = $gettok($wildtok(%check,nonce="*,1,44),2,34)
    var %response = $gettok($wildtok(%check,response="*,1,44),2,34)
    var %uri = $gettok($replace($wildtok(%check,*uri=*,1,34),uri=,uri="),2,34)
    var %opaque = $gettok($wildtok(%check,opaque="*,1,44),2,34)
    var %nc = $remove($gettok($gettok($wildtok(%check,nc=*,1,44),2,61),1,44),")
    var %cnonce = $gettok($wildtok(%check,cnonce="*,1,44),2,34)
    var %compute = $gettok($gettok($wildtok(%check,qop=*,1,44),2,61),1,34)

    var %http = $gettok($bvar(&lido,1,30).text,1,32)

    if ($hget(proxy_accgranted,%nonce) != $sock($sockname).ip) return 407

    hadd -mu3600 proxy_accgranted %nonce $sock($sockname).ip

    var %fullhost = $gettok($bget(&lido,Host:,13 10),2,32)
    var %host = $gettok(%fullhost,1,58) 

    if (!$readf(proxy_pass) && ($hget(proxy_shosts,%host) != AP)) { return ok }

    var %auth = $readf(proxy_pass)

    if ((%compute == auth) || ((qop isin %check) && !%compute)) { %auth = $md5(%auth $+ : $+ %nonce $+ : $+ %nc $+ : $+ %cnonce $+ : $+ %compute $+ : $+ $md5(%http $+ : $+ %uri)) }
    elseif (%compute == auth-int) { %auth = $md5(%auth $+ : $+ %nonce $+ : $+ %nc $+ : $+ %cnonce $+ : $+ %compute $+ : $+ $md5(%http $+ : $+ %uri $+ : $+ $md5(entity-body))) }    
    else { %auth = $md5(%auth $+ : $+ %nonce $+ : $+ $md5(%http $+ : $+ %uri)) }

    if ((%auth == %response) && ($hget(proxy_shosts,%host) != AP)) { return ok }

    %auth = $readf(parental_pass)

    if ((%compute == auth) || ((qop isin %check) && !%compute)) { %auth = $md5(%auth $+ : $+ %nonce $+ : $+ %nc $+ : $+ %cnonce $+ : $+ %compute $+ : $+ $md5(%http $+ : $+ %uri)) }
    elseif (%compute == auth-int) { %auth = $md5(%auth $+ : $+ %nonce $+ : $+ %nc $+ : $+ %cnonce $+ : $+ %compute $+ : $+ $md5(%http $+ : $+ %uri $+ : $+ $md5(entity-body))) }    
    else { %auth = $md5(%auth $+ : $+ %nonce $+ : $+ $md5(%http $+ : $+ %uri)) }

    if (%auth == %response) { return ok }
    else { return 407 }
  }
}

alias -l le_web2 {
  ;if something goes wrong, close client socket to inform browser asap
  if ($sockerr > 0) {
    cleanups 
    return
  }

  var %proxy = proxy_web_l_ $+ $ret(1)
  if (!$sock(%proxy)) { sockclose $sockname | return }
  sockread 5120 &lido

  if ($readf(proxy_logs)) bwrite $shortfn($scriptdir) $+ logs\web_ $+ $ret(1) $+ .log -1 &lido

  var %i = $gettok($hget(cabecalhos,$ret(1) $+ +),1,32)
  var %j = $gettok($hget(cabecalhos,$ret(1) $+ +),2,32)
  if (%i == $null) %i = 999999
  if (%j == $null) %j = 999999

  if ((%i != %j) || ($sock(%proxy).sq > 8192)) {
    inc %i
    hadd -b cabecalhos $ret(1) $+ + $+ %i &lido
    hadd cabecalhos $ret(1) $+ + %i $gettok($hget(cabecalhos,$ret(1) $+ +),2,32)

    %i = $gettok($hget(cabecalhos,$ret(1) $+ +),2,32)
    if (!%i) %i = 999999
    var %lixo = 1

    while (%lixo > 0) {
      inc %i
      %lixo = $hget(cabecalhos,$ret(1) $+ + $+ %i,&fich)

      if ((%lixo > 0) && ($sock(%proxy).sq < 8192)) {
        hdel cabecalhos $ret(1) $+ + $+ %i | sockwrite %proxy &fich | bunset &fich
        hadd cabecalhos $ret(1) $+ + $gettok($hget(cabecalhos,$ret(1) $+ +),1,32) %i
      }
      else { dec %i | break }
    }

    if (!$timer(proxy_+ $+ $ret(1))) { .timerproxy_+ $+ $ret(1) -om 0 500 sendToClient %proxy }
  }
  else {
    if ($readf(proxy_dcc_exploit)) {
      var %init = $bfind(&lido,1,PRIVMSG)
      var %next = $bfind(&lido,1,$+(:,$chr(1),DCC))
      if ($readf(proxy_dcc_exploit) && (%init > 0) && (%next > 0)) {
        bwrite exploit_debug.log -1 &lido
        if ($calc($bvar(&lido,0) - %init) >= 130) { echo -ta 4 Got an attack attempt from $bvar(&lido,%init,50).text }
      }
      else { sockwrite %proxy &lido }
    }
    else { sockwrite %proxy &lido }
  }
}





;###  SOCKS 5
on 1:sockclose:proxy_telnet_l_*:{ sockclose proxy_telnet_c_ $+ $ret(1) | sockclose list_ $+ $ret(1) }
on 1:sockclose:proxy_telnet_c_*:{ sockclose proxy_telnet_l_ $+ $ret(1) | sockclose list_ $+ $ret(1) }

on 1:sockread:proxy_telnet_l_*: le_telnet1
on 1:sockread:proxy_telnet_c_*: le_telnet2

on 1:socklisten:telnet: {
  var %temp = $sock(telnet).mark
  if (!%temp || %temp > 65000) { %temp = 0 }

  :mais
  inc %temp
  if ($sock(proxy_telnet_l_ $+ %temp)) { goto mais }

  sockmark telnet %temp
  sockaccept proxy_telnet_l_ $+ %temp
}

on 1:sockopen:proxy_telnet_c_*: {
  var %proxy = proxy_telnet_l_ $+ $ret(1)
  var %myip = $sock($sockname).ip
  var %port = $sock($sockname).port

  if (!$sock(%proxy)) { sockclose $sockname | return }
  if ($sockerr) { 
    if ($mid($ret(2),1,1) == 5) sockwrite %proxy $convert(5 4)
    sockclose %proxy | return 
  }

  bset &newlido 1 $ret(2)
  bset &newlido 2 0
  bset &newlido 3 0

  if ($mid($ret(2),1,1) == 5) { 
    sockwrite %proxy $convert(5 0 0 1 $replace(%myip,.,$chr(32)) $replace($gettok($longip(%port),3-,46),.,$chr(32)))
    ;sockwrite %proxy &newlido 
  }

  sockmark $sockname connected
}

on 1:socklisten:list_*: {
  var %sock = proxy_telnet_c_ $+ $ret(1)

  sockaccept %sock
  sockwrite proxy_telnet_l_ $+ $ret(1) $convert(5 0 0 1 $replace($sock(%sock).ip,.,$chr(32)) $gettok($replace($longip($sock(%sock).bindport),.,$chr(32)),3-,32))
}


alias -l le_telnet1 {
  if (($sockerr > 0) || ($sock(proxy_telnet_c_ $+ $ret(1)).sq > 8000)) return

  sockread &lido

  if ($readf(proxy_logs)) bwrite $shortfn($scriptdir) $+ logs\proxy_ $+ $ret(1) $+ .log -1 &lido

  if ($ret(2) != connected) {
    test_ip
    if (($bvar(&lido,1) == 4)) { sockclose $sockname }
    elseif (($bvar(&lido,1) == 5)) { socks5 | return }
    elseif ($bvar(&lido,1) == 1) { pass | return }
  }
  else {
    if ($sock(proxy_telnet_c_ $+ $ret(1))) {
      sockwrite proxy_telnet_c_ $+ $ret(1) &lido
    }
    else { sockclose $sockname }
  }
}


alias -l le_telnet2 {
  var %proxy = proxy_telnet_l_ $+ $ret(1)
  if (!$sock(%proxy)) { sockclose $sockname | return }
  if (($sockerr > 0) || ($sock(%proxy).sq > 8192)) return

  sockread &lido

  if ($readf(proxy_logs)) bwrite $shortfn($scriptdir) $+ logs\proxy_ $+ $ret(1) $+ .log -1 &lido

  sockwrite %proxy &lido
}


alias -l convert { bset &var 1 $1- | return &var }


alias -l socks5 {
  var %type = 0

  if ($ret(2) == $null) {
    sockmark $sockname connecting
    if ($readf(proxy_pass)) { sockwrite $sockname $convert(5 2) }
    else { sockwrite $sockname $convert(5 0) }
  }
  else if ($ret(2) == connecting) {
    var %ip, %porta, %j = 6

    if ($bvar(&lido,4) == 1) {
      %ip = $bvar(&lido,5) $+ . $+ $bvar(&lido,6) $+ . $+ $bvar(&lido,7) $+ . $+ $bvar(&lido,8)
      %porta = $longip(0.0. $+ $bvar(&lido,9) $+ . $+ $bvar(&lido,10))
      %type = 1
    }
    elseif ($bvar(&lido,4) == 3) {
      %ip = $bvar(&lido,%j,$bvar(&lido,5)).text

      inc %j $bvar(&lido,5)
      %porta = $longip(0.0. $+ $bvar(&lido,%j) $+ . $+ $bvar(&lido,$calc(%j + 1)))
      %type = 3
    }
    else {
      ;# i am not supporting IP V6 address yet (ever??)
      sockwrite $sockname $convert(5 8 0 1 0 0 0 0 0 0)
      sockclose $sockname
      return
    }

    if ($bvar(&lido,2) == 1) {

      sockopen proxy_telnet_c_ $+ $ret(1) %ip %porta
      sockmark proxy_telnet_c_ $+ $ret(1) $bvar(&lido,1,100)
      sockmark $sockname connected

      ;# mensagem e enviada quando o socket e aberto, ver error_test
    }
    elseif ($bvar(&lido,2) == 2) {
      var %port_aux = 2211
      while (!$portfree(%port_aux)) { inc %port_aux }

      socklisten list_ $+ $ret(1) %port_aux
      %porta = $sock(list_ $+ $ret(1)).port
      %porta = $longip(%porta)
      %porta = $replace(%porta,.,$chr(32))
      %ip = $replace($sock(list_ $+ $ret(1)).ip,.,$chr(32))

      sockwrite $sockname $convert(5 0 0 1 %ip $gettok(%porta,3-,32))
      sockmark $sockname connected
      echo @tontito 3 $sockname Listening in ip %ip with $gettok(%porta,3-,32)
    }
    else {
      ;# Udp not supported
      sockwrite $sockname $convert(5 7 0 1 0 0 0 0 0 0)
      sockclose $sockname
      return
    }
  }
}

alias -l pass {
  var %user, %pass, %j = 3, %fixo = 2
  %user = $bvar(&lido,%j,$bvar(&lido,%fixo)).text
  inc %j $bvar(&lido,%fixo)
  %fixo = %j | inc %j
  %pass = $bvar(&lido,%j,$bvar(&lido,%fixo)).text

  if ($md5($remove(%user $+ :Tontito Proxy Server Authenticate System: $+ %pass,=)) == $readf(proxy_pass)) {
    sockwrite $sockname $convert(1 0)
  }
}


alias -l conf {
  if (!$readf(parental_pass)) { if (!$dialog(proxy_setup)) dialog -md proxy_setup proxy_setup }
  else {
    var %pass = $$?*="Please type your access password."
    if ($md5(:Tontito Proxy Server Authenticate System $+ : $+ %pass) == $readf(parental_pass)) { if (!$dialog(proxy_setup)) dialog -md proxy_setup proxy_setup }
  }
}


;### DIALOGS
dialog proxy_setup {
  title "Proxy Setup"
  size -1 -1 213 242
  option dbu
  tab "General", 1, 2 0 206 220
  box "Server setup", 2, 7 17 201 70, tab 1
  edit "", 4, 42 71 49 10, tab 1
  text "Set Lan Ip", 5, 12 72 30 8, tab 1
  text "Set Lan Mask", 6, 106 72 38 8, tab 1
  edit "", 7, 144 71 49 10, tab 1
  edit "", 8, 167 39 26 10, tab 1 limit 5
  text "Set http listening port", 9, 106 40 54 8, tab 1
  check "Enable Socket Logging", 10, 12 97 79 10, tab 1
  check "Enable http listening", 11, 12 39 74 10, tab 1
  text "Set socks5 listening port", 3, 106 56 60 8, tab 1
  edit "", 12, 167 55 26 10, tab 1 limit 5
  box "Utils", 13, 7 90 200 45, tab 1
  button "Start listening", 14, 41 115 46 12, tab 1
  button "Stop server", 15, 124 115 46 12, tab 1
  text "", 24, 107 25 84 8, tab 1
  check "Enable Autostart", 29, 12 25 67 10, tab 1
  check "Enable soks5 listening", 28, 12 54 74 10, tab 1
  tab "Security", 16
  check "Set Authentication", 23, 12 44 78 10, tab 16
  text "", 27, 13 31 188 8, tab 16
  edit "", 19, 45 60 50 10, tab 16
  text "Server Login", 21, 12 61 33 8, tab 16
  text "Server Password", 22, 111 61 41 8, tab 16
  box "Proxy Password", 18, 7 19 200 60, tab 16
  edit "", 20, 153 60 50 10, tab 16 pass
  box "Dcc Exploit", 31, 7 83 200 33, tab 16
  check "Enable Dcc Exploit protection (mirc connects to irc server using this proxy)", 32, 12 94 192 10, tab 16
  box "Bock file extensions", 40, 7 120 199 55, tab 16
  button "Add extension", 51, 74 134 40 12, tab 16
  button "Remove", 52, 74 153 40 12, tab 16
  check "Enable block", 53, 12 132 50 10, tab 16
  list 46, 133 127 54 44, tab 16 size vsbar
  check "Strong Authentication (Digest)", 56, 100 44 101 10, tab 16
  tab "Routing...", 30
  box "Mappings", 17, 7 17 198 81, tab 30
  combo 38, 76 74 60 48, tab 30 sort size drop
  button "Add Mapping", 39, 19 31 45 12, tab 30
  edit "", 41, 178 31 20 10, tab 30
  edit "", 42, 105 51 45 10, tab 30
  edit "", 43, 178 51 20 10, tab 30
  text "Inc Port", 44, 153 32 24 8, tab 30
  text "Dest Port", 45, 153 52 23 8, tab 30
  text "Forward to", 47, 78 52 28 8, tab 30
  button "Remove Mapping", 48, 19 74 45 12, tab 30
  text "Label", 49, 78 32 18 8, tab 30
  edit "", 50, 105 31 37 10, tab 30
  tab "Parental Settings", 54
  box "Block pages", 33, 6 22 199 43, tab 54
  button "Add Host", 34, 13 37 40 12, tab 54
  button "Remove Host", 35, 159 37 40 12, tab 54
  combo 36, 62 37 90 50, tab 54 sort size drop
  link "Edit Host", 37, 65 54 90 8, tab 54
  box "Block words", 55, 6 74 198 58, tab 54
  button "Ok", 25, 44 227 37 12, ok
  button "Cancel", 26, 120 227 37 12, cancel
}

on *:dialog:proxy_setup:init:*:{ fill_conf }

on *:dialog:proxy_setup:sclick:*: {
  if ($did == 23) {
    var %login

    if ($did(proxy_setup,19) && $did(proxy_setup,20)) { %login = $md5($did(proxy_setup,19) $+ :Tontito Proxy Server Authenticate System: $+ $did(proxy_setup,20)) }

    if (($did(proxy_setup,23).state == 1) && %login) { did -a proxy_setup 27 Server login and password set! | $writef(proxy_pass,%login) }
    elseif (($did(proxy_setup,23).state == 1) && (!%login)) { did -r proxy_setup 19,20 | did -u proxy_setup 23 | did -a proxy_setup 27 Please add login and password first! | $remf(proxy_pass) }
    else { did -re proxy_setup 19,20 | $remf(proxy_pass) | did -r proxy_setup 27 }
  }

  if ($did == 14) { 
    $writef(proxy_http_port,$did(proxy_setup,8))
    $writef(proxy_socks5_port,$did(proxy_setup,12))
    var %begin, %end      
    %begin = $and($longip($did(proxy_setup,4)),$longip($did(proxy_setup,7)))
    %end = $or(%begin,$not($longip($did(proxy_setup,7))))
    $writef(begin_ip,%begin) | $writef(end_ip,%end) | $writef(proxy_lip,$did(proxy_setup,4)) | $writef(proxy_lmask,$did(proxy_setup,7))

    if ($did(proxy_setup,11).state == 1) $writef(proxy_http,yes)
    else $remf(proxy_http)

    if ($did(proxy_setup,28).state == 1) $writef(proxy_socks5,yes)
    else $remf(proxy_socks5)

    escuta 
  }
  if ($did == 15) { para }

  if ($did == 32) { 
    if ($did(proxy_setup,32).state == 1) {
      .firewall -cmp-d on 127.0.0.1 $readf(proxy_http_port)
      $writef(proxy_dcc_exploit,on)
      If ($readf(proxy_pass)) { echo 7 -a Please go to Options -> connect -> firewall and setup the user and pass of this proxy! }
    }
    else { .firewall off | $remf(proxy_dcc_exploit) }
  }

  if ($did == 53) { 
    if ($did(proxy_setup,53).state == 1) {
      $writef(filter_extensions,on)
    }
    else {
      $writef(filter_extensions,off)
    }
  }

  ;## ok
  if ($did == 25) {

    if (($numtok($did(proxy_setup,4),46) == 4) && ($longip($did(proxy_setup,4)) isnum) && ($numtok($did(proxy_setup,7),46) == 4) && ($longip($did(proxy_setup,7)) isnum)) { 
      var %begin, %end      
      %begin = $and($longip($did(proxy_setup,4)),$longip($did(proxy_setup,7)))
      %end = $or(%begin,$not($longip($did(proxy_setup,7))))
      $writef(begin_ip,%begin) | $writef(end_ip,%end) | $writef(proxy_lip,$did(proxy_setup,4)) | $writef(proxy_lmask,$did(proxy_setup,7))
    }
    else { did -a proxy_setup 24 Invalid Ip or Mask! | halt }

    if ($did(proxy_setup,10).state == 1) $writef(proxy_logs,yes)
    else $remf(proxy_logs)

    if ($did(proxy_setup,11).state == 1) {
      $writef(proxy_http,yes)
    }
    else $remf(proxy_http)

    if ($did(proxy_setup,28).state == 1) {
      $writef(proxy_socks5,yes)
    }
    else $remf(proxy_socks5)


    if ($did(proxy_setup,29).state == 1) $writef(proxy_autostart,yes)
    else $remf(proxy_autostart)
  }

  if ($did == 34) {
    if (!$readf(parental_pass)) {
      %tmp = $$?*="!!!!Please 1º type your new access password!!!! (will also be used to access your config menu from now on.)"
      %tmp = $remove(%tmp,=)
      $writef(parental_pass,$md5(:Tontito Proxy Server Authenticate System $+ : $+ %tmp))
    }

    var %quest = $$?="What is the host name you want to add?"
    var %type = $$?="Want to tottaly block or allow access with parental pass? (BL or AP)"

    if ((%type == BL) || (%type == AP)) {
      hadd -m proxy_shosts $remove(%quest,http://) %type
      hsave -o proxy_shosts $shortfn($scriptdir) $+ configs\Hosts.ric
      load_comb
    }
  }

  if ($did == 35) {
    if ($did(proxy_setup,36)) {
      hdel proxy_shosts $did(proxy_setup,36)
      hsave -o proxy_shosts $shortfn($scriptdir) $+ configs\Hosts.ric
      load_comb
    }
  }

  if ($did == 37) {
    if ($did(proxy_setup,36)) {
      var %quest = $$?="Please change the Host"
      var %type = $$?="Want to tottaly block or allow access with parental pass? (BL or AP)"

      if ((%type == BL) || (%type == AP)) {    
        hdel proxy_shosts $did(proxy_setup,36)
        hadd -m proxy_shosts $remove(%quest,http://) %type
        hsave -o proxy_shosts $shortfn($scriptdir) $+ configs\Hosts.ric
        load_comb
      }
    }
  }

  if ($did == 38) {
    did -r proxy_setup 41,42,43,50
    var %value = $hget(proxy_routes,$did(proxy_setup,38).seltext)
    did -a proxy_setup 41 $gettok(%value,1,58)
    did -a proxy_setup 42 $gettok(%value,2,58)
    did -a proxy_setup 43 $gettok(%value,3,58)
    did -a proxy_setup 50 $did(proxy_setup,38).seltext
  }

  ;## add
  if ($did == 39) {
    if ($did(proxy_setup,41) && $did(proxy_setup,42) && $did(proxy_setup,43) && $did(proxy_setup,50)) {
      hadd -m proxy_routes $did(proxy_setup,50) $did(proxy_setup,41) $+ : $+ $did(proxy_setup,42) $+ : $+ $did(proxy_setup,43)
      hsave -o proxy_routes $shortfn($scriptdir) $+ configs\Routes.ric
      load_comb2
    }
  }

  ;## remove
  if ($did == 48) {
    if ($did(proxy_setup,38).seltext) {
      did -r proxy_setup 41,42,43,50
      hdel proxy_routes $did(proxy_setup,38).seltext
      hsave -o proxy_routes $shortfn($scriptdir) $+ configs\Routes.ric

      if ($sock(route_ $+ $did(proxy_setup,38).seltext)) {
        sockclose route_ $+ $did(proxy_setup,38).seltext
        echo 4 closing routing service for $did(proxy_setup,38).seltext
      }

      load_comb2
    }
  }

  if ($did == 51) {
    var %ext = $$?="Add new Extension"
    hadd -m proxy_ext %ext ok
    hsave -o proxy_ext $shortfn($scriptdir) $+ configs\Extensions.ric
    load_comb3
  }

  if (($did == 52) && ($did(proxy_setup,46).seltext)) {
    hdel proxy_ext $did(proxy_setup,46).seltext
    hsave -o proxy_ext $shortfn($scriptdir) $+ configs\Extensions.ric
    load_comb3
  }

  if ($did == 56) {
    if (($did(proxy_setup,56).state == 1)) { $writef(authdigest,1) }
    else { $remf(authdigest) }
  }
}

alias -l load_comb {
  did -r proxy_setup 36
  var %i = 1
  while (%i <= $hget(proxy_shosts,0).item) {
    if (($hget(proxy_shosts,%i).data == BL) || ($hget(proxy_shosts,%i).data == AP)) { did -a proxy_setup 36 $hget(proxy_shosts,%i).item } 
    inc %i
  }
}

alias -l load_comb2 {
  did -r proxy_setup 38
  var %i = 1
  while (%i <= $hget(proxy_routes,0).item) {
    did -a proxy_setup 38 $hget(proxy_routes,%i).item | inc %i
  }
}

alias -l load_comb3 {
  did -r proxy_setup 46
  var %i = 1
  while (%i <= $hget(proxy_ext,0).item) {
    did -a proxy_setup 46 $hget(proxy_ext,%i).item | inc %i
  }
}


; ### lan_ip


on *:dialog:proxy_setup:edit:*: {
  if (($did == 4) || ($did == 7)) { did -r proxy_setup 24 }

  if ($did == 8) {
    if (($did(proxy_setup,8) !isnum) || ($did(proxy_setup,8) >= 65500) || ($did(proxy_setup,8) <= 0)) { did -a proxy_setup 24 Invalid http port number! }
    else {
      did -r proxy_setup 24 | $writef(proxy_http_port,$did(proxy_setup,8)) 
      if ($did(proxy_setup,11).state == 1) {
        sockclose proxy
        socklisten proxy $did(proxy_setup,8)
      }
    }
  }

  if ($did == 12) {
    if (($did(proxy_setup,12) !isnum) || ($did(proxy_setup,12) >= 65500) || ($did(proxy_setup,12) <= 0)) { did -a proxy_setup 24 Invalid soks5 port number! }
    else {
      did -r proxy_setup 24 | $writef(proxy_socks5_port,$did(proxy_setup,12)) 
      if ($did(proxy_setup,28).state == 1) {
        sockclose telnet
        socklisten telnet $did(proxy_setup,12)
      }
    }
  }

  if (($did == 19) || ($did == 20)) {
    if ($did(proxy_setup,19) && $did(proxy_setup,20)) {
      if ($did(proxy_setup,23).state != 1) { did -c proxy_setup 23 }

      did -a proxy_setup 27 Server login and password set!
      var %login = $md5($remove($did(proxy_setup,19) $+ :Tontito Proxy Server Authenticate System: $+ $did(proxy_setup,20),=))
      $writef(proxy_pass,%login)
    }
  }
}


alias -l fill_conf {
  if ($readf(proxy_pass)) { did -c proxy_setup 23 | did -a proxy_setup 27 Server login and password set! | did -b proxy_setup 19,20 }
  if ($readf(proxy_logs)) { did -c proxy_setup 10 }
  if ($readf(proxy_http)) { did -c proxy_setup 11 }
  if ($readf(proxy_socks5)) { did -c proxy_setup 28 }

  if ($readf(proxy_http_port)) did -a proxy_setup 8 $readf(proxy_http_port)
  else { $writef(proxy_http_port,8080) | did -a proxy_setup 8 8080 }

  if ($readf(proxy_socks5_port)) did -a proxy_setup 12 $readf(proxy_socks5_port)
  else { $writef(proxy_socks5_port,1080) | did -a proxy_setup 12 1080 }

  if ($readf(proxy_lip)) did -a proxy_setup 4 $readf(proxy_lip)
  else did -a proxy_setup 4 $ip

  if ($readf(proxy_lmask)) did -a proxy_setup 7 $readf(proxy_lmask)
  else did -a proxy_setup 7 255.255.255.0

  if ($readf(proxy_autostart)) did -c proxy_setup 29
  if ($readf(proxy_dcc_exploit)) did -c proxy_setup 32
  if ($readf(filter_extensions) == on) did -c proxy_setup 53

  if ($readf(authdigest) == 1) did -c proxy_setup 56

  load_comb
  load_comb2
  load_comb3
}


:### ROUTING
on 1:sockclose:route_*_s_*: { sockclose $replace($sockname,_s_,_c_) }
on 1:sockclose:route_*_c_*: { sockclose $replace($sockname,_c_,_s_) }

on 1:sockread:route_*_c_*: le_route1
on 1:sockread:route_*_s_*: le_route2

on 1:sockwrite:route_*: { }

on 1:socklisten:route_*: aceita_route
on 1:sockopen:route_*: connect_route


alias -l aceita_route {
  var %service = $gettok($sockname,2,95)
  if (!$hget(proxy_routes,%service)) { return }

  var %temp = $sock($sockname).mark
  if (!%temp || %temp > 65000) { %temp = 0 }

  inc %temp

  sockmark $sockname %temp
  sockaccept $sockname $+ _c_ $+ %temp
  sockopen $replace($sockname $+ _c_ $+ %temp,_c_,_s_) $gettok($hget(proxy_routes,%service),2,58) $gettok($hget(proxy_routes,%service),3,58)
}

alias -l connect_route {
  var %other = $replace($sockname,_s_,_c_)

  if (!$sock(%other)) { sockclose $sockname | return }
  if ($sockerr) { sockclose %other | sockclose $sockname | return }
}

alias -l le_route1 {
  var %other = $replace($sockname,_c_,_s_)

  if (!$sock(%other)) { sockclose $sockname | return }
  if ($sockerr) { sockclose %other | return }

  ;only buffers initial connecting data from client
  if (($sock(%other).status != active)) {
    sockread 1024 &lido
    bcopy -c &tmp $bvar(&tmp,0) &lido 1 -1
    hadd -mb web_proxy_partial $sockname &tmp

    if ($readf(proxy_logs)) bwrite $shortfn($scriptdir) $+ logs\route_ $+ $ret(1) $+ .log -1 &lido
    return
  }
  elseif ($hget(web_proxy_partial,$sockname,&tmp)) {
    hdel web_proxy_partial $sockname
    sockwrite %ligado &tmp
  }

  sockread 512 &lido
  sockwrite %other &lido

  if ($readf(proxy_logs)) bwrite $shortfn($scriptdir) $+ logs\route_ $+ $ret(1) $+ .log -1 &lido
}

alias -l le_route2 {
  var %other = $replace($sockname,_s_,_c_)

  if (!$sock(%other)) { sockclose $sockname | return }
  if ($sockerr) { sockclose %other | return }

  sockread 512 &lido
  sockwrite %other &lido

  if ($readf(proxy_logs)) bwrite $shortfn($scriptdir) $+ logs\route_ $+ $ret(1) $+ .log -1 &lido
}


;### BINVAR HANDLING ALIAS 
; $breplacer(bin,query,delimiter,replacer)
alias -l breplacer {
  var %tmpz1 = $bfind($1,1,$2)
  if ((%tmpz1 == 0) || !$4) { return }
  bunset &binvarxpt342
  bset -t &binvarxpt555 1 $4
  var %tmpz2 = $bfind($1,%tmpz1,$3)

  bcopy -c &binvarxpt342 1 $1 1 $calc(%tmpz1 - 1)
  bcopy -c &binvarxpt342 %tmpz1 &binvarxpt555 1 -1
  bcopy -c &binvarxpt342 $calc(%tmpz1 + $len($4)) $1 %tmpz2 -1

  bunset $1
  bcopy -c $1 1 &binvarxpt342 1 -1

  bunset &binvarxpt342
  bunset &binvarxpt555
}

; $bremover(bin,query,delimiter)
alias -l bremover {
  var %tamass = $bvar($1,0)
  var %tmpz1,%tmpz2,%tmpz3

  bunset &binvarxpt342 &binvarxpt555
  bset &binvarxpt555 1 $3
  %tmpz1 = $bfind($1,1,$2)
  if (%tmpz1 == 0) { bunset &binvarxpt555 | return }

  %tmpz2 = $bfind($1,%tmpz1,$3)
  bcopy -c &binvarxpt342 1 $1 1 $calc(%tmpz1 - 1)
  bcopy -c &binvarxpt342 %tmpz1 $1 $calc(%tmpz2 + $bvar(&binvarxpt555,0)) -1

  bunset $1
  bcopy -c $1 1 &binvarxpt342 1 -1
  bunset &binvarxpt342 &binvarxpt555
}

; $bget(bin,query,delimiter) there are text size limitations
alias -l bget {
  var %a = $bfind($1,1,$2)

  if (%a == 0) { return }

  var %b = $bfind($1,%a,$3)
  if ($calc(%b - %a) > 4130) { return }

  return $bvar($1,%a,$calc(%b - %a)).text
}

; $bnget(bin,ntok,delimiter) there are text size limitations if > 4130 bytes
alias -l bnget {
  var %tmpz1,%tmpz2, %i = 1
  bunset &binvarxpt555
  bset &binvarxpt555 1 $3

  %tmpz1 = $bfind($1,1,$3)
  %tmpz2 = %tmpz1

  if ($2 > 1) {
    while ($true) {
      %tmpz2 = $bfind($1,$calc(%tmpz1 + 1),$3)
      inc %i
      if (%i == $2) break
      %tmpz1 = %tmpz2
    }
    %tmpz1 = $calc(%tmpz1 + $bvar(&binvarxpt555,0))
  }
  else %tmpz1 = 1

  if ((%tmpz1 > %tmpz2) || $calc(%tmpz2 - %tmpz1) > 4130) {
    return -1
  }

  bunset &binvarxpt555
  return $bvar($1,%tmpz1,$calc(%tmpz2 - %tmpz1)).text
}

;For html processing (mainly)
alias getHeader {
  var %i = $bfind($2, 1, $1)
  if (!%i) { return }

  %i = $calc(%i + $len($1))
  return $bvar($2,%i,$calc($bfind($2, %i, 13 10) - %i)).text
}

; $posbnget(bin,ntok,delimiter) returns position of 1º char in token ntok position
alias -l posbnget {
  var %tmpz1, %tmpz2, %i = 1

  bunset &binvarxpt555
  bset &binvarxpt555 1 $3

  %tmpz1 = $bfind($1,1,$3)
  %tmpz2 = %tmpz1

  if ($2 > 1) {
    while ($true) {
      %tmpz2 = $bfind($1,$calc(%tmpz1 + 1),$3)
      inc %i
      if (%i == $2) break
      %tmpz1 = %tmpz2
    }
  }
  else %tmpz1 = -1

  return $calc(%tmpz1 + $bvar(&binvarxpt555,0))
}


;$bconcat(bin,$bvar1,$bvar2,$bspacer) concats text with bvar
alias -l bconcat {
  bunset &binvarxpt342 &binvarxpt555
  bset -t &binvarxpt342 1 $2

  if ($4) { 
    bset &binvarxpt555 1 $4
    bcopy -c &binvarxpt342 $calc($bvar(&binvarxpt342,0) + 1) &binvarxpt555 1 -1
    bunset &binvarxpt555
  }

  bcopy -c &binvarxpt342 $calc($bvar(&binvarxpt342,0) + 1) $3 1 -1

  bunset $1
  bcopy -c $1 1 &binvarxpt342 1 -1
  bunset &binvarxpt342 $3
}


;$getBvar(bin,n,delimiter) parses the data using delimiter and gets the data after the n token incuding the delimiter at start!
alias -l getBvar {
  if (!$3) return
  bunset &exit1
  var %i = 1, %j = $calc($bvar(&lido,0) - $posbnget(&lido,$2,$3) + 2), %inc = 1

  while (%i <= %j) {
    if ($calc(%j - %i) > 180) %inc = 180
    else %inc = $calc(%j - %i + 1)

    bset &exit1 %i $bvar(&lido, $calc($posbnget(&lido,$2,$3) + %i - 2), %inc)
    %i = $calc(%i + %inc)
  }

  return &exit1
}



;### VERSION CHECKER
alias -l checkupdate {
  ; never add http:// to the connection socket ...
  sockopen proxy_update $+ $ticks tontito.no-ip.com 80
}

on *:sockclose:proxy_update*: { }

on *:sockopen:proxy_update*: {
  if ($sockerr) { echo -a . $sock($sockname).wsmsg | return }
  sockwrite -n $sockname GET versions/versions.txt HTTP/1.1
  sockwrite -n $sockname Host: tontito.no-ip.com
  sockwrite -n $sockname User-Agent: Proxy Server $gettok($read(" $+ $script $+ ",5),3,32) on $version and $os
  sockwrite -n $sockname Pragma: no-cache
  sockwrite -n $sockname Cache-Control: no-cache
  sockwrite -n $sockname
}

on *:sockread:proxy_update*: {
  if ($sockerr > 0) { return }
  var %i, %version
  sockread -n %i

  if ((version isin %i) && ($chr(35) !isin %i) && (:proxy: isin %i)) {
    var %proxy_version = $gettok($read($script,5),3,32)
    %proxy_version = $gettok(%proxy_version,1-2,46) $+ $gettok(%proxy_version,3,46)
    %version = $gettok(%i,3,58)
    var %get_where = $gettok(%i,4,58)
    var %rnotes = $gettok(%i,5,58)

    if (%get_where == email) %get_where = Plz email me (tontitoscript@gmail.com) case you are interested in testing it and give new ideas.
    elseif (%get_where == offline) %get_where = File is not available for download yet!
    elseif (%get_where == mircscripts) %get_where = There is a new Web Server version3 %version at http://www.mircscripts.org/download.php?id=2120
    else %get_where = at http://tontito.no-ip.com/versions/ $+ %get_where Plz report bugs to tontitoscript@gmail.com

    if ((%version isnum) && (%version > %proxy_version)) echo 7 -a %get_where
    elseif (beta isin %version) echo 7 -a There is a recent (beta) version %version $+  %get_where
    elseif (RC isincs %version) echo 3 -a There is a (Release Candidate) version %version $+  %get_where
    else echo -a 3,15 You have the most current stable Proxy Server version!
  }

  if (%rnotes) { echo -a 3,7 Release Notes: %rnotes }
}
;##