sxSockets — File Browser

sxSocketsv1.5.5 / sxSocketsv1.5.5 / sxSockets.mrc

sxSockets.mrc — 14.08 KB — Download this file

; --------< Simple Script >----------------------------------------------------
; Name....: sxSockets
; File....: sxSockets.mrc
; Ver.....: v1.5.5
; Author..: sprion
; Email...: sprion@lyniq.com
; Date....: 24th August 2004
; Info....: Sockets with proxy + logging capabilities
; Changes.: Cleaned up codes
;---------------------------------------------------------------------

/*
To use, summarized:
Works almost like the built in SOCKETS of mIRC

1. use '/sxSockopen -d <sockname> ? ? ? ?' to create the connection

2. then create the events: (only these 2 works, on sockopen doesn't)
      'on *:sockread:<sockname>:actions' event
      'on *:sockclose:<sockname>:actions' event

      * these are recommended over the SIGNAL counterparts because of speed

3. There are also events signals:

      on *:signal:sxSocks.open:
          to compensate the missing 'on sockopen' event

      on *:signal:sxSocks.read:
          same as 'on sockread' event, only cannot use /sockread &r to store,
          and triggered by all sxSockets.

          use: 'bcopy &read 1 $sxsockread($1) 1 -1' to store read data in binary var &read

      on *:signal:sxSocks.close:
          same as 'on sockclose' event, except it is triggered by all sxSockets

      on *:signal:sxSocks.fail:

      on *:signal:sxSocks.info:
          shows protocol communication

more details & example in readme.txt

*/
; SCRIPT FILE DO NOT EDIT
alias sxsock.unload unload -rs $script
alias sxsock.log return $+($scriptdir,sxsocklogs\)
alias sxsock.forgetall {
  var %f = $+(",$sxsock.log,"),%z = sxSocks.*,%n = $findfile(%f,%z,0)
  if (!%n) { goto b }
  if ($input(Would you like to clear all sxSockets log files?,136,Unloading)) {
    while (%n > 0) {
      .remove $+(",$findfile(%f,%z,%n),")
      dec %n
    }
    goto b
  }
  return
  :b
  rmdir $+(",$sxsock.log,")
}
alias sxsockForget {
  var %d = $sxsock.hand($1)
  .remove $+(",$sxsock.log,%d,.data,")
  .remove $+(",$sxsock.log,%d,.index,")
}
alias sxsock {
  var %n = $1
  if ($1 = Current) {
    %n = $hget(sxSocks,Current)
    if (!$prop) return %n
    tokenize 32 %n
  }
  %n = $sxsock.hand($1)
  if (!$prop) return $hget(sxSocks,%n)
  else return $hget(sxSocks,$+(%n,_,$prop))
}
alias sxsockSet {
  var %n = -
  if ($2 = -p) { %n = $+($1,_,$3) | tokenize 32 $3- }
  else %n = $1
  hadd sxSocks %n $2-
}
alias sxsockRead {
  var %i = $+(",$sxsock.log,$1,.index,")
  if ($2 = 0) return $lines(%i)
  var %n = $iif($2,$2,$sxSock($1).index)
  bread $+(",$sxsock.log,$1,.data,") $gettok($read(%i,%n),1-2,32) &a
  sxSockSet $1 -p index $calc(%n + 1)
  return &a
}
alias sxsockClose {
  var %d = $sxsock.hand($1)
  if $sock(%d) {
    sockclose %d
    sockclose W. $+ %d
    sxsockmemforget %d
    .signal -n sxSocks.info Socket(s) has been terminated. Use /sxsockForget <name> to remove log.
  }
}
alias sxsockOpen {
  mkdir $+(",$sxsock.log,")
  var %n -
  if $1 = $null {
    goto error2
  }
  elseif $1 = -d {
    if $2 = Current {
      sx.e error: Current is a reserved word, please use another name.
      return
    }
    var %n = $sxsock.hand($2)
    tokenize 32 $3-
  }
  var %t $right($1,-1)
  if ((%t != h) && (%t != 4) && (%t != 5)) goto error2

  if (!$hget(sxSocks)) sxsock.createhash

  ; socket name provided
  if (%n != -) {
    hadd sxSocks %n Connecting
  }
  ; or not
  else {
    var %r = $+(.,$r(a,z),$base($r(0,9999),10,10,4))
    %n = $+(sxSocks,%r)
  }
  sxSockSet Current %n

  sxSockSet %n Connecting
  sxSockSet %n -p index 1
  sxSockSet %n -p param $1-
  sxSockSet %n -p method %t

  sxSockSet %n -p proxyIP $2
  sxSockSet %n -p proxyPort $3
  sxSockSet %n -p remoteIP $4
  sxSockSet %n -p remotePort $5

  sxSockSet %n -p user $6
  sxSockSet %n -p pass $7

  sxSockSet %n -p proxyName $right(%n,-8)
  sxSockSet %n -p start $ticks

  ; SOCKS4 doesn't support named addresses
  if (!$longip($4) && %t = 4) {
    .enable #sxSocketDNS
    .dns $4
    goto end
  }

  sockopen %n $2 $3
  %r = 57410
  while !$portfree(%r) {
    %r = $r(1024,5000)
  }
  if (!$sock(sxSockets)) socklisten -d 127.0.0.1 sxSockets %r
  sockopen W. $+ %n 127.0.0.1 %r
  .signal -n sxSocks.info Connecting: %n
  goto end
  ;  :error
  ;  sx.e error: $gettok($error,3-,32)
  ;  goto end
  :error2
  sx.e error: sxsockopen [-d Name] <+h45> <proxy_addr/ip> <port> <target_addr/ip> <port> [user] [pass]
  sx.e tip: [] optional - <> required - specify 1 proxy only - $sx.g
  :end
}
alias sxsocklist {
  var %d = $sxsock.hand($iif($1,$1,*)), %n = $sock(%d,0)
  echo -e -
  if (%n = 0) { sx.e sxSocklist: No open sxSockets | goto e }
  sx.e sxSocklist: Listing %n socket(s)..
  while %n > 0 {
    var %p = $sock(%d,%n)
    sx.e sxSocklist: $right(%p,-8) ( $+ %p $+ ) - $+($sock(%p).bindip,:,$sock(%p).bindport) > $+($sock(%p).ip,:,$sock(%p).port) $sock(%p).status
    dec %n
  }
  :e
  echo -e -
}
alias -l sxsock.hand if ($left($1,8) = sxSocks.) return $1 | else return $+(sxSocks.,$1)
alias -l sxsockmemforget {
  hdel -w sxSocks $+($1,_,*)
  hdel sxSocks $1
}

alias -l sxsock.createhash {
  if (!$hget(sxSocks)) hmake sxSocks 10
  hadd sxSocks Current -
}
alias -l sxsock.socks4gen {
  if (!$1) || (!$2) { return }
  var %c = $base($2,10,16,4) | tokenize 46 $1
  var %e = 0
  if ($3) {
    if ($sxSock($3).user) %e = $sxsock.break($ifmatch) 0
  }
  return 4 1 $base($mid(%c,1,2),16,10) $base($mid(%c,3,2),16,10) $1 $2 $3 $4 %e
}
alias -l sxsock.socks5gen {
  if (!$1) || (!$2) { return }
  var %c = $base($2,10,16,4), %d = $len($1)
  if ($longip($1)) {
    tokenize 46 $1
    return 5 1 0 1 $1 $2 $3 $4 $base($mid(%c,1,2),16,10) $base($mid(%c,3,2),16,10)
  }
  else {
    return 5 1 0 3 %d $sxsock.break($1) $base($mid(%c,1,2),16,10) $base($mid(%c,3,2),16,10)
  }
}
alias -l sxsock.break {
  if ($1) {
    var %r, %c = 0, %d = $len($1)
    while (%c < %d) {
      inc %c
      set %r %r $+ $asc($mid($1,%c,1)) $+ $str($chr(32),2)
    }
    return %r
    ;return $replace(%r,@+,$chr(32))
  }
}

/*
#############
Events
#############
*/
on 1:unload:{
  sxsock.forgetall
  if ($hget(sxSocks)) hfree sxSocks
  sx.e notice: Thank you for appreciating sprion :P
}
#sxSocketDNS off
on 1:DNS:{
  .disable #sxSocketDNS
  if ($dns(0) > 0) {
    echo -e * Resolved $sxSock(Current).remoteIP to $dns(1).ip - proceeding..
    sxsockopen -d $sxSock(Current) $replace($sxSock(Current).param,$sxSock(Current).remoteIP,$dns(1).ip)
  }
  else .signal -n sxSocks.fail $sxSock(Current) Unable to resolve $sxSock(Current).remoteIP
  haltdef
  halt
}
#sxSocketDNS end

on *:socklisten:sxSockets:sockaccept $sxsock(Current).proxyName | sockclose sxSockets

on *:sockopen:sxSocks.*:{
  if ($sockerr > 0) { .signal -n sxSocks.fail $sockname Unable to connect | return }
  .signal -n sxSocks.info $sockname Connected to proxy.. forgetting previous log of this connection.
  sxsockforget $sockname
  if ($sxSock($sockname).method = 4) {
    sxSockSet $sockname connect
    bset &socks4 1 $sxsock.socks4gen($sxSock($sockname).remoteIP,$sxSock($sockname).remotePort,$sockname)
    sockwrite -b $sockname $bvar(&socks4,0) &socks4
  }
  elseif ($sxSock($sockname).method = h) {
    .signal -n sxSocks.info $sockname HTTP sending connect string
    sxSockSet $sockname connect
    sockwrite $sockname CONNECT $sxSock($sockname).remoteIP $+ : $+ $sxSock($sockname).remotePort HTTP/1.0 $+ $crlf $+ $crlf
  }
  elseif ($sxSock($sockname).method = 5) {
    .signal -n sxSocks.info $sockname SOCKS5 sending version and method of authentication(no auth)
    sxSockSet $sockname Authenticating
    ;    no authentication..
    ;    bset &socks5 1 5 1 0
    bset &socks5 1 5 2 0 2
    ; this line sends 2 choices.. no auth and with auth .. u're lucky if proxy allows no auth :P
    sockwrite -b $sockname $bvar(&socks5,0) &socks5
  }
}
on *:sockclose:sxSocks.*:{
  sxsockmemforget $sockname
  ; this line below will call proxy's sockclose event
  sockclose W. $+ $sockname
  .signal -n sxSocks.close $sockname
  ;  unset %sxsock.tun.*
}
on *:sockread:sxSocks.*:{
  if ($sockerr > 0) return
  :nextread
  if ($sxSock($sockname) = Connected) {
    sockread &sockread
    %sxsock.ticks = $ticks
    sockwrite W. $+ $sockname &sockread
    if ($bvar(&sockread,0) != 0) {
      write $+(",$sxsock.log,$sockname,.index,") $iif($file($+(",$sxsock.log,$sockname,.data,")).size,$ifmatch,0) $bvar(&sockread,0) $calc($ticks - $sxsock($sockname).start)
      bwrite $+(",$sxsock.log,$sockname,.data,") -1 -1 &sockread
      .signal -n sxSocks.read $sockname
    }
    if ($sockbr == 0) return
    goto nextread
  }

  elseif ($sxSock($sockname).method = h) {
    var %r, %h HTTP
    sockread -f %r | tokenize 32 %r
    if ($1 = HTTP/1.0) {
      if ($2 != 200 && $2 != 206) { .signal -n sxSocks.fail $sockname %h Error $2 | sxsockclose $sockname | return }
      sxSockSet $sockname Connected
      .signal -n sxSocks.open $sockname %h $sxSock($sockname).remoteIP $sxSock($sockname).proxyIP NA
    }
  }
  elseif ($sxSock($sockname).method = 5) {
    sockread &sockread
    var %5 SOCKS5, %r $bvar(&sockread,1,$bvar(&sockread,0))
    if ($sxSock($sockname) = Authenticating) && (%r = 5 0) {
      .signal -n sxSocks.info $sockname %5 Accepted Auth Method: 0x0 (No Auth)
      .signal -n sxSocks.info $sockname %5 Sending Command Request - Connect
      sxSockSet $sockname connect
      bset &socks5 1 $sxsock.socks5gen($sxSock($sockname).remoteIP,$sxSock($sockname).remotePort)
      sockwrite -b $sockname $bvar(&socks5,0) &socks5
    }
    elseif ($sxSock($sockname) = Authenticating) && (%r = 5 2) {
      var %u = $sxSock($sockname).user, %p = $sxSock($sockname).pass
      if ((!%u) || (!%p)) {
        .signal -n sxSocks.fail $sockname %5 Username/Password required.
        sxsockclose $sockname
        return
      }
      .signal -n sxSocks.info $sockname %5 Accepted Auth Method: 0x2 (Username/Password)
      .signal -n sxSocks.info $sockname %5 Sending User Authentication Response
      sxSockSet $sockname Authenticating2
      bset &socks5 1 1 $len(%u) $sxsock.break(%u) $len(%p) $sxsock.break(%p)
      sockwrite -b $sockname $bvar(&socks5,0) &socks5
    }
    elseif ($sxSock($sockname) = Authenticating2) && (%r = 1 1) {
      .signal -n sxSocks.fail $sockname %5 General SOCKS server failure (auth fail)
    }
    elseif ($sxSock($sockname) = Authenticating2) && (%r = 1 0) {
      .signal -n sxSocks.info $sockname %5 Authentication Successful
      .signal -n sxSocks.info $sockname %5 Sending Command Request - Connect
      sxSockSet $sockname connect
      bset &socks5 1 $sxsock.socks5gen($sxSock($sockname).remoteIP,$sxSock($sockname).remotePort)
      sockwrite -b $sockname $bvar(&socks5,0) &socks5
    }

    elseif ($sxSock($sockname) = connect) && (5 0 0* iswm %r) {
      sxSockSet $sockname Connected
      .signal -n sxSocks.open $sockname %5 $sxSock($sockname).remoteIP $replace($bvar(&sockread,5,4),$chr(32),$chr(46)) $base($base($bvar(&sockread,9),10,16,2) $+ $base($bvar(&sockread,10),10,16,2),16,10)
    }
    ;sx.e value of sockread: $bvar(&sockread,1,$bvar(&sockread,0))
  }

  elseif ($sxSock($sockname).method = 4) {
    sockread &sockread4
    if ($sxSock($sockname) = connect) && (0 90* iswm $bvar(&sockread4,1,$bvar(&sockread4,0))) {
      ;      sx.e socks4: Connected to $sxSock($sockname,remoteIP) from $replace($bvar(&sockread4,5,4),$chr(32),$chr(46)) $base($base($bvar(&sockread4,3),10,16,2) $+ $base($bvar(&sockread4,4),10,16,2),16,10)
      sxSockSet $sockname Connected
      .signal -n sxSocks.open $sockname SOCKS4 $sxSock($sockname).remoteIP $replace($bvar(&sockread4,5,4),$chr(32),$chr(46)) $base($base($bvar(&sockread4,3),10,16,2) $+ $base($bvar(&sockread4,4),10,16,2),16,10)
    }
    ;sx.e value of sockread4: $bvar(&sockread4,1,$bvar(&sockread4,0))
  }
  if ($sockbr == 0) return
  goto nextread
}


; STUFFS
alias sxsock.update if ($sock(sx.u)) { sx.e notice: another instance running, aborted | return } | dialog -m z sxu | sx.u
alias -l sx.e { if ($1 = -) var %c = $chr(160) | else var %c = 12,15-=- $+  $+ $1  $+ $color(highlight text) $+ $2- | echo -tai16 %c }
; UPDATER
alias -l sx.c return $gettok($read(%sx.w,9),2-,58)
alias -l sx.r return $gettok($read($1-,4),2-,58)
alias -l sx.g return $sx.r(%sx.o)
alias -l sx.t return $sx.r(%sx.w)
alias -l sx.u.a if ($1) sx.s No updates found. | sockclose sx.u | .remove %sx.w | unset %sx.*
alias -l sx.u {
  unset %sx.* | %sx.o = $+(",$script,") | %sx.w = $+(",$script,.t,") | %sx.n = $nopath($script)
  %sx.a = zion.lyniq.com | %sx.f = /scripts/ $+ $remove(%sx.n,.mrc)
  %sx.u = $+(http://,%sx.a,%sx.f,/,%sx.n)
  sx.s Connecting...
  .timersx.u 0 1 sx.i $!r(1,5)
  %sx.m = Update failed.
  .remove %sx.w
  sockopen sx.u %sx.a 80
}
on *:sockopen:sx.u:{
  if ($sockerr) { sx.s %sx.m | sx.u.a | return }
  var %z sockwrite -tn sx.u
  %z GET %sx.u HTTP/1.1 | %z Host: %sx.a | %z Accept: *.*, */* | %z Range: bytes=0- | %z Connection: close | %z $crlf
}
on *:sockclose:sx.u:sx.u.a
on *:sockread:sx.u:{
  if ($sockerr) { sx.s %sx.m | sx.u.a | return }
  if (%sx.s != d) {
    var %1 | sockread %1 | tokenize 32 %1
    if ($1 == HTTP/1.1) { if ($2 != 200 && $2 != 206) { sx.s %sx.m | sockclose sx.u } }
    elseif ($1 == Content-Range:) { %sx.z = $gettok($3,-1,47) }
    elseif (!$1) {
      %sx.s = d
      goto d
    }
  }
  else {
    :d
    var %1 %sx.w,%2 %sx.z
    :sockread
    sockread &1
    if (!$sockbr) { return }
    bwrite %1 -1 &1
    var %3 $file(%1).size
    sx.s Downloading... $int($calc(%3 / %2 *100)) $+ % $+([,%3,/,%2,])
    sx.i $calc(%3 / %2 *46)
    if (!%sx.g && $sx.t) { if ($ifmatch <= $sx.g) { sx.u.a 1 | return } | else %sx.g = 1 }
    if (%3 < %2) { goto sockread }
    else {
      sx.i 0
      sx.s Update success! $cr Changes: $sx.g > $sx.t $cr $sx.c
      dialog -sb z -1 -1 118 30
      .copy -o %sx.w %sx.o | .timer 1 1 .reload -rs %sx.o | sx.u.a
    }
  }
}
dialog sxu {
  title %sx.n Updater
  size -1 -1 118 18
  option dbu
  text "", 2, 2 3 120 25
  text "", 1, 2 10 120 8
}
alias -l sx.s .timersx.u off | did -a z 2 $1-
alias -l sx.i did -a z 1 $str(•,$1)