Scripts/Pump

From J Wiki
Jump to navigation Jump to search

Wm yes check.png

Pump: multiPort synchronoUs Multiplexing Pump routs TCP connections and requests to particular servers through designated ports of other servers. Good to fool VPN to expose its machines to non-member connections.

History

I wasn't able to figure out how to configure this sort of operation in Windows XP so instead I wrote this script that pretty well does the job.

Design

  • Based on my unpublished HTTP server
  • Included accept into read loop
  • Pair sequence of dual sockets
  • Detailed but compact file logging
  • Junk code cleanup using aliases

Issues

  • Write in a separate loop
  • Not validating receiving socket

The Code

Information.png NO WARRANTIES EXTENDED. PROVIDED AS IS. USE AT YOUR OWN RISK.

[{{#file: "pump.ijs"}} Download script: pump.ijs ]

NB. pump - multiPort synchronoUs Multiplexing Pump
NB.
NB. install   copy 'pump.ijs' to '~user/'
NB. config    modify 'config' verb
NB. execute   load 'user/pump.ijs'
NB.           run''
NB. browse    http://machinename:800/dir/file
NB. stop      delete '~temp/httpj-stop' file
NB. author    06/08/2006 Oleg Kobchenko

require 'socket files'
coinsert 'jdefs jsocket'

scgethostbyname =: sdcheck@sdgethostbyname
sccleanup =:       sdcheck@sdcleanup
scsocket  =: 0&{::@sdcheck@sdsocket
scbind    =:       sdcheck@sdbind
sclisten  =:       sdcheck@sdlisten
scconnect =:       sdcheck@sdconnect
srselect  =: 0&{::@sdcheck@sdselect
swselect  =: 1&{::@sdcheck@sdselect
scaccept  =: 0&{::@sdcheck@sdaccept
scioctl   =: 0&{::@sdcheck@sdioctl
upto=: (<. #) {. ]

config=: 3 : 0
  Port=: 800 801
  RemotePort=: 80 80
  RemoteAddr=: 'jsoftware.com';'www.google.com'
  Conns=: 10
  ChunkSize=: <.2^16
  Timeout=: 200
  Semaphor=: jpath'~temp/httpj-stop'
  Log=: jpath'~temp/httpj-log.txt'
)

init=: 3 : 0
  log=: [: fappend&Log ,&LF
  sccleanup ''
  SH=: 0 2$0
  SL=: i.0
  for_i. i.#RemoteAddr do.
    rip=. scgethostbyname i{::RemoteAddr
    SH=: SH, rip,<i{RemotePort
    SL=: SL,s=. scsocket ''
    scbind s;AF_INET;'';i{Port
    sclisten s,Conns
  end.
  sel SK=: ''
)

sel=: 3 : 'selarg=: (SK,SL);'''';'''';Timeout'

dual=: 3 : '{.y-.~SK{~(,>:)(-2&|)SK i.y'

add=: 4 : 0
  s=. scsocket ''
  scconnect s;x{SH
  sel SK=: SK, y,s
  log 'add (',(":x),') ',":y,s,#SK
)

remove=: 3 : 0
  sel SK=: SK-. close y,s=. dual y
  log 'remove ',":y,s,#SK
)

close=: 3 : 0"0
  shutdownJ y;0
  shutdownJ y;2
  y [ sdclose y
)

loop=: 3 : 0
  '' fwrite Semaphor
  log 'begin ',":SL
  nl=. #SL
  while. fexist Semaphor do.
    for_s. srselect selarg do.
      if. nl>il=. SL i.s do. il add scaccept s
      elseif. -.s e.SK do. 0    NB. removed dual?
      elseif. pump s do. remove s end.
    end.
  end.
  log 'end ',":SL
)

pump=: 3 : 0  NB. x.<-y.
  if. 0=n=. scioctl y,FIONREAD,0 do.
    log 'read oef'
    1 return.
  end.
  'error d'=. sdrecv y,n
  if. error+.0=#d do.
    log 'read error/oef'
    1 return.
  end.
  log (":y),'->',":#d
  d write dual y
  0
)

write=: 4 : 0
  sa=. '';(,y);'';Timeout
  nsa=. '' [ err=. 0
  while. (fexist Semaphor) *. *#x do.
    if. y e. swselect sa do.
      'err numsent'=. (ChunkSize upto x) sdsend y,0
      nsa=. nsa,numsent
      x=. numsent}.x
      if. err do. break. end.
    end.
  end.
  log (":y),'<-',":nsa
  if. 0< err + #x do.
    log 'write underflow ',":err,#x end.
  err
)

run=: sccleanup @ loop @ init @ config

Contributed by Oleg Kobchenko