[2747] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ Ajaxterm """ |
---|
| 4 | |
---|
[2761] | 5 | import array,cgi,fcntl,glob,hashlib,mimetypes,optparse,os,pty,random,re,signal,select,sys,threading,time,termios,struct,pwd |
---|
[2747] | 6 | |
---|
| 7 | os.chdir(os.path.normpath(os.path.dirname(__file__))) |
---|
| 8 | # Optional: Add QWeb in sys path |
---|
| 9 | sys.path[0:0]=glob.glob('../../python') |
---|
| 10 | |
---|
| 11 | import qweb |
---|
| 12 | |
---|
| 13 | class Terminal: |
---|
| 14 | def __init__(self,width=80,height=24): |
---|
| 15 | self.width=width |
---|
| 16 | self.height=height |
---|
| 17 | self.init() |
---|
| 18 | self.reset() |
---|
| 19 | def init(self): |
---|
| 20 | self.esc_seq={ |
---|
| 21 | "\x00": None, |
---|
| 22 | "\x05": self.esc_da, |
---|
| 23 | "\x07": None, |
---|
| 24 | "\x08": self.esc_0x08, |
---|
| 25 | "\x09": self.esc_0x09, |
---|
| 26 | "\x0a": self.esc_0x0a, |
---|
| 27 | "\x0b": self.esc_0x0a, |
---|
| 28 | "\x0c": self.esc_0x0a, |
---|
| 29 | "\x0d": self.esc_0x0d, |
---|
| 30 | "\x0e": None, |
---|
| 31 | "\x0f": None, |
---|
| 32 | "\x1b#8": None, |
---|
| 33 | "\x1b=": None, |
---|
| 34 | "\x1b>": None, |
---|
| 35 | "\x1b(0": None, |
---|
| 36 | "\x1b(A": None, |
---|
| 37 | "\x1b(B": None, |
---|
| 38 | "\x1b[c": self.esc_da, |
---|
| 39 | "\x1b[0c": self.esc_da, |
---|
| 40 | "\x1b]R": None, |
---|
| 41 | "\x1b7": self.esc_save, |
---|
| 42 | "\x1b8": self.esc_restore, |
---|
| 43 | "\x1bD": None, |
---|
| 44 | "\x1bE": None, |
---|
| 45 | "\x1bH": None, |
---|
| 46 | "\x1bM": self.esc_ri, |
---|
| 47 | "\x1bN": None, |
---|
| 48 | "\x1bO": None, |
---|
| 49 | "\x1bZ": self.esc_da, |
---|
| 50 | "\x1ba": None, |
---|
| 51 | "\x1bc": self.reset, |
---|
| 52 | "\x1bn": None, |
---|
| 53 | "\x1bo": None, |
---|
| 54 | } |
---|
| 55 | for k,v in self.esc_seq.items(): |
---|
| 56 | if v==None: |
---|
| 57 | self.esc_seq[k]=self.esc_ignore |
---|
| 58 | # regex |
---|
| 59 | d={ |
---|
| 60 | r'\[\??([0-9;]*)([@ABCDEFGHJKLMPXacdefghlmnqrstu`])' : self.csi_dispatch, |
---|
| 61 | r'\]([^\x07]+)\x07' : self.esc_ignore, |
---|
| 62 | } |
---|
| 63 | self.esc_re=[] |
---|
| 64 | for k,v in d.items(): |
---|
| 65 | self.esc_re.append((re.compile('\x1b'+k),v)) |
---|
| 66 | # define csi sequences |
---|
| 67 | self.csi_seq={ |
---|
| 68 | '@': (self.csi_at,[1]), |
---|
| 69 | '`': (self.csi_G,[1]), |
---|
| 70 | 'J': (self.csi_J,[0]), |
---|
| 71 | 'K': (self.csi_K,[0]), |
---|
| 72 | } |
---|
| 73 | for i in [i[4] for i in dir(self) if i.startswith('csi_') and len(i)==5]: |
---|
| 74 | if not self.csi_seq.has_key(i): |
---|
| 75 | self.csi_seq[i]=(getattr(self,'csi_'+i),[1]) |
---|
| 76 | # Init 0-256 to latin1 and html translation table |
---|
| 77 | self.trl1="" |
---|
| 78 | for i in range(256): |
---|
| 79 | if i<32: |
---|
| 80 | self.trl1+=" " |
---|
| 81 | elif i<127 or i>160: |
---|
| 82 | self.trl1+=chr(i) |
---|
| 83 | else: |
---|
| 84 | self.trl1+="?" |
---|
| 85 | self.trhtml="" |
---|
| 86 | for i in range(256): |
---|
| 87 | if i==0x0a or (i>32 and i<127) or i>160: |
---|
| 88 | self.trhtml+=chr(i) |
---|
| 89 | elif i<=32: |
---|
| 90 | self.trhtml+="\xa0" |
---|
| 91 | else: |
---|
| 92 | self.trhtml+="?" |
---|
| 93 | def reset(self,s=""): |
---|
| 94 | self.scr=array.array('i',[0x000700]*(self.width*self.height)) |
---|
| 95 | self.st=0 |
---|
| 96 | self.sb=self.height-1 |
---|
| 97 | self.cx_bak=self.cx=0 |
---|
| 98 | self.cy_bak=self.cy=0 |
---|
| 99 | self.cl=0 |
---|
| 100 | self.sgr=0x000700 |
---|
| 101 | self.buf="" |
---|
| 102 | self.outbuf="" |
---|
| 103 | def peek(self,y1,x1,y2,x2): |
---|
| 104 | return self.scr[self.width*y1+x1:self.width*y2+x2] |
---|
| 105 | def poke(self,y,x,s): |
---|
| 106 | pos=self.width*y+x |
---|
| 107 | self.scr[pos:pos+len(s)]=s |
---|
| 108 | def zero(self,y1,x1,y2,x2): |
---|
| 109 | w=self.width*(y2-y1)+x2-x1+1 |
---|
| 110 | z=array.array('i',[0x000700]*w) |
---|
| 111 | self.scr[self.width*y1+x1:self.width*y2+x2+1]=z |
---|
| 112 | def scroll_up(self,y1,y2): |
---|
| 113 | self.poke(y1,0,self.peek(y1+1,0,y2,self.width)) |
---|
| 114 | self.zero(y2,0,y2,self.width-1) |
---|
| 115 | def scroll_down(self,y1,y2): |
---|
| 116 | self.poke(y1+1,0,self.peek(y1,0,y2-1,self.width)) |
---|
| 117 | self.zero(y1,0,y1,self.width-1) |
---|
| 118 | def scroll_right(self,y,x): |
---|
| 119 | self.poke(y,x+1,self.peek(y,x,y,self.width)) |
---|
| 120 | self.zero(y,x,y,x) |
---|
| 121 | def cursor_down(self): |
---|
| 122 | if self.cy>=self.st and self.cy<=self.sb: |
---|
| 123 | self.cl=0 |
---|
| 124 | q,r=divmod(self.cy+1,self.sb+1) |
---|
| 125 | if q: |
---|
| 126 | self.scroll_up(self.st,self.sb) |
---|
| 127 | self.cy=self.sb |
---|
| 128 | else: |
---|
| 129 | self.cy=r |
---|
| 130 | def cursor_right(self): |
---|
| 131 | q,r=divmod(self.cx+1,self.width) |
---|
| 132 | if q: |
---|
| 133 | self.cl=1 |
---|
| 134 | else: |
---|
| 135 | self.cx=r |
---|
| 136 | def echo(self,c): |
---|
| 137 | if self.cl: |
---|
| 138 | self.cursor_down() |
---|
| 139 | self.cx=0 |
---|
| 140 | self.scr[(self.cy*self.width)+self.cx]=self.sgr|ord(c) |
---|
| 141 | self.cursor_right() |
---|
| 142 | def esc_0x08(self,s): |
---|
| 143 | self.cx=max(0,self.cx-1) |
---|
| 144 | def esc_0x09(self,s): |
---|
| 145 | x=self.cx+8 |
---|
| 146 | q,r=divmod(x,8) |
---|
| 147 | self.cx=(q*8)%self.width |
---|
| 148 | def esc_0x0a(self,s): |
---|
| 149 | self.cursor_down() |
---|
| 150 | def esc_0x0d(self,s): |
---|
| 151 | self.cl=0 |
---|
| 152 | self.cx=0 |
---|
| 153 | def esc_save(self,s): |
---|
| 154 | self.cx_bak=self.cx |
---|
| 155 | self.cy_bak=self.cy |
---|
| 156 | def esc_restore(self,s): |
---|
| 157 | self.cx=self.cx_bak |
---|
| 158 | self.cy=self.cy_bak |
---|
| 159 | self.cl=0 |
---|
| 160 | def esc_da(self,s): |
---|
| 161 | self.outbuf="\x1b[?6c" |
---|
| 162 | def esc_ri(self,s): |
---|
| 163 | self.cy=max(self.st,self.cy-1) |
---|
| 164 | if self.cy==self.st: |
---|
| 165 | self.scroll_down(self.st,self.sb) |
---|
| 166 | def esc_ignore(self,*s): |
---|
| 167 | pass |
---|
| 168 | # print "term:ignore: %s"%repr(s) |
---|
| 169 | def csi_dispatch(self,seq,mo): |
---|
| 170 | # CSI sequences |
---|
| 171 | s=mo.group(1) |
---|
| 172 | c=mo.group(2) |
---|
| 173 | f=self.csi_seq.get(c,None) |
---|
| 174 | if f: |
---|
| 175 | try: |
---|
| 176 | l=[min(int(i),1024) for i in s.split(';') if len(i)<4] |
---|
| 177 | except ValueError: |
---|
| 178 | l=[] |
---|
| 179 | if len(l)==0: |
---|
| 180 | l=f[1] |
---|
| 181 | f[0](l) |
---|
| 182 | # else: |
---|
| 183 | # print 'csi ignore',c,l |
---|
| 184 | def csi_at(self,l): |
---|
| 185 | for i in range(l[0]): |
---|
| 186 | self.scroll_right(self.cy,self.cx) |
---|
| 187 | def csi_A(self,l): |
---|
| 188 | self.cy=max(self.st,self.cy-l[0]) |
---|
| 189 | def csi_B(self,l): |
---|
| 190 | self.cy=min(self.sb,self.cy+l[0]) |
---|
| 191 | def csi_C(self,l): |
---|
| 192 | self.cx=min(self.width-1,self.cx+l[0]) |
---|
| 193 | self.cl=0 |
---|
| 194 | def csi_D(self,l): |
---|
| 195 | self.cx=max(0,self.cx-l[0]) |
---|
| 196 | self.cl=0 |
---|
| 197 | def csi_E(self,l): |
---|
| 198 | self.csi_B(l) |
---|
| 199 | self.cx=0 |
---|
| 200 | self.cl=0 |
---|
| 201 | def csi_F(self,l): |
---|
| 202 | self.csi_A(l) |
---|
| 203 | self.cx=0 |
---|
| 204 | self.cl=0 |
---|
| 205 | def csi_G(self,l): |
---|
| 206 | self.cx=min(self.width,l[0])-1 |
---|
| 207 | def csi_H(self,l): |
---|
| 208 | if len(l)<2: l=[1,1] |
---|
| 209 | self.cx=min(self.width,l[1])-1 |
---|
| 210 | self.cy=min(self.height,l[0])-1 |
---|
| 211 | self.cl=0 |
---|
| 212 | def csi_J(self,l): |
---|
| 213 | if l[0]==0: |
---|
| 214 | self.zero(self.cy,self.cx,self.height-1,self.width-1) |
---|
| 215 | elif l[0]==1: |
---|
| 216 | self.zero(0,0,self.cy,self.cx) |
---|
| 217 | elif l[0]==2: |
---|
| 218 | self.zero(0,0,self.height-1,self.width-1) |
---|
| 219 | def csi_K(self,l): |
---|
| 220 | if l[0]==0: |
---|
| 221 | self.zero(self.cy,self.cx,self.cy,self.width-1) |
---|
| 222 | elif l[0]==1: |
---|
| 223 | self.zero(self.cy,0,self.cy,self.cx) |
---|
| 224 | elif l[0]==2: |
---|
| 225 | self.zero(self.cy,0,self.cy,self.width-1) |
---|
| 226 | def csi_L(self,l): |
---|
| 227 | for i in range(l[0]): |
---|
| 228 | if self.cy<self.sb: |
---|
| 229 | self.scroll_down(self.cy,self.sb) |
---|
| 230 | def csi_M(self,l): |
---|
| 231 | if self.cy>=self.st and self.cy<=self.sb: |
---|
| 232 | for i in range(l[0]): |
---|
| 233 | self.scroll_up(self.cy,self.sb) |
---|
| 234 | def csi_P(self,l): |
---|
| 235 | w,cx,cy=self.width,self.cx,self.cy |
---|
| 236 | end=self.peek(cy,cx,cy,w) |
---|
| 237 | self.csi_K([0]) |
---|
| 238 | self.poke(cy,cx,end[l[0]:]) |
---|
| 239 | def csi_X(self,l): |
---|
| 240 | self.zero(self.cy,self.cx,self.cy,self.cx+l[0]) |
---|
| 241 | def csi_a(self,l): |
---|
| 242 | self.csi_C(l) |
---|
| 243 | def csi_c(self,l): |
---|
| 244 | #'\x1b[?0c' 0-8 cursor size |
---|
| 245 | pass |
---|
| 246 | def csi_d(self,l): |
---|
| 247 | self.cy=min(self.height,l[0])-1 |
---|
| 248 | def csi_e(self,l): |
---|
| 249 | self.csi_B(l) |
---|
| 250 | def csi_f(self,l): |
---|
| 251 | self.csi_H(l) |
---|
| 252 | def csi_h(self,l): |
---|
| 253 | if l[0]==4: |
---|
| 254 | pass |
---|
| 255 | # print "insert on" |
---|
| 256 | def csi_l(self,l): |
---|
| 257 | if l[0]==4: |
---|
| 258 | pass |
---|
| 259 | # print "insert off" |
---|
| 260 | def csi_m(self,l): |
---|
| 261 | for i in l: |
---|
| 262 | if i==0 or i==39 or i==49 or i==27: |
---|
| 263 | self.sgr=0x000700 |
---|
| 264 | elif i==1: |
---|
| 265 | self.sgr=(self.sgr|0x000800) |
---|
| 266 | elif i==7: |
---|
| 267 | self.sgr=0x070000 |
---|
| 268 | elif i>=30 and i<=37: |
---|
| 269 | c=i-30 |
---|
| 270 | self.sgr=(self.sgr&0xff08ff)|(c<<8) |
---|
| 271 | elif i>=40 and i<=47: |
---|
| 272 | c=i-40 |
---|
| 273 | self.sgr=(self.sgr&0x00ffff)|(c<<16) |
---|
| 274 | # else: |
---|
| 275 | # print "CSI sgr ignore",l,i |
---|
| 276 | # print 'sgr: %r %x'%(l,self.sgr) |
---|
| 277 | def csi_r(self,l): |
---|
| 278 | if len(l)<2: l=[0,self.height] |
---|
| 279 | self.st=min(self.height-1,l[0]-1) |
---|
| 280 | self.sb=min(self.height-1,l[1]-1) |
---|
| 281 | self.sb=max(self.st,self.sb) |
---|
| 282 | def csi_s(self,l): |
---|
| 283 | self.esc_save(0) |
---|
| 284 | def csi_u(self,l): |
---|
| 285 | self.esc_restore(0) |
---|
| 286 | def escape(self): |
---|
| 287 | e=self.buf |
---|
| 288 | if len(e)>32: |
---|
| 289 | # print "error %r"%e |
---|
| 290 | self.buf="" |
---|
| 291 | elif e in self.esc_seq: |
---|
| 292 | self.esc_seq[e](e) |
---|
| 293 | self.buf="" |
---|
| 294 | else: |
---|
| 295 | for r,f in self.esc_re: |
---|
| 296 | mo=r.match(e) |
---|
| 297 | if mo: |
---|
| 298 | f(e,mo) |
---|
| 299 | self.buf="" |
---|
| 300 | break |
---|
| 301 | # if self.buf=='': print "ESC %r\n"%e |
---|
| 302 | def write(self,s): |
---|
| 303 | for i in s: |
---|
| 304 | if len(self.buf) or (i in self.esc_seq): |
---|
| 305 | self.buf+=i |
---|
| 306 | self.escape() |
---|
| 307 | elif i == '\x1b': |
---|
| 308 | self.buf+=i |
---|
| 309 | else: |
---|
| 310 | self.echo(i) |
---|
| 311 | def read(self): |
---|
| 312 | b=self.outbuf |
---|
| 313 | self.outbuf="" |
---|
| 314 | return b |
---|
| 315 | def dump(self): |
---|
| 316 | r='' |
---|
| 317 | for i in self.scr: |
---|
| 318 | r+=chr(i&255) |
---|
| 319 | return r |
---|
| 320 | def dumplatin1(self): |
---|
| 321 | return self.dump().translate(self.trl1) |
---|
[2761] | 322 | def dumphtml(self,color=1,last_hash=None): |
---|
[2747] | 323 | h=self.height |
---|
| 324 | w=self.width |
---|
| 325 | r="" |
---|
| 326 | span="" |
---|
| 327 | span_bg,span_fg=-1,-1 |
---|
| 328 | for i in range(h*w): |
---|
| 329 | q,c=divmod(self.scr[i],256) |
---|
| 330 | if color: |
---|
| 331 | bg,fg=divmod(q,256) |
---|
| 332 | else: |
---|
| 333 | bg,fg=0,7 |
---|
| 334 | if i==self.cy*w+self.cx: |
---|
| 335 | bg,fg=1,7 |
---|
| 336 | if (bg!=span_bg or fg!=span_fg or i==h*w-1): |
---|
| 337 | if len(span): |
---|
| 338 | r+='<span class="f%d b%d">%s</span>'%(span_fg,span_bg,cgi.escape(span.translate(self.trhtml))) |
---|
| 339 | span="" |
---|
| 340 | span_bg,span_fg=bg,fg |
---|
| 341 | span+=chr(c) |
---|
| 342 | if i%w==w-1: |
---|
| 343 | span+='\n' |
---|
[2761] | 344 | hash = hashlib.md5(r).hexdigest() |
---|
| 345 | r='<?xml version="1.0" encoding="ISO-8859-1"?><pre class="term" id="%s">%s</pre>'% (hash,r) |
---|
| 346 | if last_hash == hash: |
---|
[2747] | 347 | return '<?xml version="1.0"?><idem></idem>' |
---|
| 348 | else: |
---|
| 349 | return r |
---|
| 350 | def __repr__(self): |
---|
| 351 | d=self.dumplatin1() |
---|
| 352 | r="" |
---|
| 353 | for i in range(self.height): |
---|
| 354 | r+="|%s|\n"%d[self.width*i:self.width*(i+1)] |
---|
| 355 | return r |
---|
| 356 | |
---|
| 357 | class SynchronizedMethod: |
---|
| 358 | def __init__(self,lock,orig): |
---|
| 359 | self.lock=lock |
---|
| 360 | self.orig=orig |
---|
| 361 | def __call__(self,*l): |
---|
| 362 | self.lock.acquire() |
---|
| 363 | r=self.orig(*l) |
---|
| 364 | self.lock.release() |
---|
| 365 | return r |
---|
| 366 | |
---|
| 367 | class Multiplex: |
---|
[2748] | 368 | def __init__(self): |
---|
[2747] | 369 | self.proc={} |
---|
| 370 | self.lock=threading.RLock() |
---|
| 371 | self.thread=threading.Thread(target=self.loop) |
---|
[2749] | 372 | self.thread.daemon=True |
---|
[2747] | 373 | self.alive=1 |
---|
| 374 | # synchronize methods |
---|
| 375 | for name in ['create','fds','proc_read','proc_write','dump','die','run']: |
---|
| 376 | orig=getattr(self,name) |
---|
| 377 | setattr(self,name,SynchronizedMethod(self.lock,orig)) |
---|
| 378 | self.thread.start() |
---|
[2748] | 379 | def create(self,cmd,w=80,h=25): |
---|
[2747] | 380 | pid,fd=pty.fork() |
---|
| 381 | if pid==0: |
---|
| 382 | try: |
---|
| 383 | fdl=[int(i) for i in os.listdir('/proc/self/fd')] |
---|
| 384 | except OSError: |
---|
| 385 | fdl=range(256) |
---|
| 386 | for i in [i for i in fdl if i>2]: |
---|
| 387 | try: |
---|
| 388 | os.close(i) |
---|
| 389 | except OSError: |
---|
| 390 | pass |
---|
| 391 | env={} |
---|
| 392 | env["COLUMNS"]=str(w) |
---|
| 393 | env["LINES"]=str(h) |
---|
| 394 | env["TERM"]="linux" |
---|
| 395 | env["PATH"]=os.environ['PATH'] |
---|
| 396 | os.execvpe(cmd[0],cmd,env) |
---|
| 397 | else: |
---|
| 398 | fcntl.fcntl(fd, fcntl.F_SETFL, os.O_NONBLOCK) |
---|
| 399 | # python bug http://python.org/sf/1112949 on amd64 |
---|
| 400 | fcntl.ioctl(fd, struct.unpack('i',struct.pack('I',termios.TIOCSWINSZ))[0], struct.pack("HHHH",h,w,0,0)) |
---|
| 401 | self.proc[fd]={'pid':pid,'term':Terminal(w,h),'buf':'','time':time.time()} |
---|
| 402 | return fd |
---|
| 403 | def die(self): |
---|
| 404 | self.alive=0 |
---|
| 405 | def run(self): |
---|
| 406 | return self.alive |
---|
| 407 | def fds(self): |
---|
| 408 | return self.proc.keys() |
---|
| 409 | def proc_kill(self,fd): |
---|
| 410 | if fd in self.proc: |
---|
| 411 | self.proc[fd]['time']=0 |
---|
| 412 | t=time.time() |
---|
| 413 | for i in self.proc.keys(): |
---|
| 414 | t0=self.proc[i]['time'] |
---|
| 415 | if (t-t0)>120: |
---|
| 416 | try: |
---|
| 417 | os.close(i) |
---|
| 418 | os.kill(self.proc[i]['pid'],signal.SIGTERM) |
---|
| 419 | except (IOError,OSError): |
---|
| 420 | pass |
---|
| 421 | del self.proc[i] |
---|
| 422 | def proc_read(self,fd): |
---|
| 423 | try: |
---|
| 424 | t=self.proc[fd]['term'] |
---|
| 425 | t.write(os.read(fd,65536)) |
---|
| 426 | reply=t.read() |
---|
| 427 | if reply: |
---|
| 428 | os.write(fd,reply) |
---|
| 429 | self.proc[fd]['time']=time.time() |
---|
| 430 | except (KeyError,IOError,OSError): |
---|
| 431 | self.proc_kill(fd) |
---|
| 432 | def proc_write(self,fd,s): |
---|
| 433 | try: |
---|
| 434 | os.write(fd,s) |
---|
| 435 | except (IOError,OSError): |
---|
| 436 | self.proc_kill(fd) |
---|
[2761] | 437 | def dump(self,fd,color=1,last_hash=None): |
---|
[2747] | 438 | try: |
---|
[2761] | 439 | return self.proc[fd]['term'].dumphtml(color, last_hash) |
---|
[2747] | 440 | except KeyError: |
---|
| 441 | return False |
---|
| 442 | def loop(self): |
---|
| 443 | while self.run(): |
---|
| 444 | fds=self.fds() |
---|
| 445 | i,o,e=select.select(fds, [], [], 1.0) |
---|
| 446 | for fd in i: |
---|
| 447 | self.proc_read(fd) |
---|
| 448 | if len(i): |
---|
| 449 | time.sleep(0.002) |
---|
| 450 | for i in self.proc.keys(): |
---|
| 451 | try: |
---|
| 452 | os.close(i) |
---|
| 453 | os.kill(self.proc[i]['pid'],signal.SIGTERM) |
---|
| 454 | except (IOError,OSError): |
---|
| 455 | pass |
---|
| 456 | |
---|
| 457 | class AjaxTerm: |
---|
| 458 | def __init__(self,cmd=None,index_file='ajaxterm.html'): |
---|
| 459 | self.files={} |
---|
| 460 | for i in ['css','html','js']: |
---|
| 461 | for j in glob.glob('*.%s'%i): |
---|
| 462 | self.files[j]=file(j).read() |
---|
| 463 | self.files['index']=file(index_file).read() |
---|
| 464 | self.mime = mimetypes.types_map.copy() |
---|
| 465 | self.mime['.html']= 'text/html; charset=UTF-8' |
---|
| 466 | self.multi = Multiplex(cmd) |
---|
| 467 | self.session = {} |
---|
| 468 | def __call__(self, environ, start_response): |
---|
| 469 | req = qweb.QWebRequest(environ, start_response,session=None) |
---|
| 470 | if req.PATH_INFO.endswith('/u'): |
---|
| 471 | s=req.REQUEST["s"] |
---|
| 472 | k=req.REQUEST["k"] |
---|
| 473 | c=req.REQUEST["c"] |
---|
| 474 | w=req.REQUEST.int("w") |
---|
| 475 | h=req.REQUEST.int("h") |
---|
| 476 | if s in self.session: |
---|
| 477 | term=self.session[s] |
---|
| 478 | else: |
---|
| 479 | if not (w>2 and w<256 and h>2 and h<100): |
---|
| 480 | w,h=80,25 |
---|
| 481 | term=self.session[s]=self.multi.create(w,h) |
---|
| 482 | if k: |
---|
| 483 | self.multi.proc_write(term,k) |
---|
| 484 | time.sleep(0.002) |
---|
| 485 | dump=self.multi.dump(term,c) |
---|
| 486 | req.response_headers['Content-Type']='text/xml' |
---|
| 487 | if isinstance(dump,str): |
---|
| 488 | req.write(dump) |
---|
| 489 | req.response_gzencode=1 |
---|
| 490 | else: |
---|
| 491 | del self.session[s] |
---|
| 492 | req.write('<?xml version="1.0"?><idem></idem>') |
---|
| 493 | # print "sessions %r"%self.session |
---|
| 494 | else: |
---|
| 495 | n=os.path.basename(req.PATH_INFO) |
---|
| 496 | if n in self.files: |
---|
| 497 | req.response_headers['Content-Type'] = self.mime.get(os.path.splitext(n)[1].lower(), 'application/octet-stream') |
---|
| 498 | req.write(self.files[n]) |
---|
| 499 | else: |
---|
| 500 | req.response_headers['Content-Type'] = 'text/html; charset=UTF-8' |
---|
| 501 | req.write(self.files['index']) |
---|
| 502 | return req |
---|
| 503 | |
---|
| 504 | def main(): |
---|
| 505 | parser = optparse.OptionParser() |
---|
| 506 | parser.add_option("-p", "--port", dest="port", default="8022", help="Set the TCP port (default: 8022)") |
---|
| 507 | parser.add_option("-c", "--command", dest="cmd", default=None,help="set the command (default: /bin/login or ssh localhost)") |
---|
| 508 | parser.add_option("-l", "--log", action="store_true", dest="log",default=0,help="log requests to stderr (default: quiet mode)") |
---|
| 509 | parser.add_option("-d", "--daemon", action="store_true", dest="daemon", default=0, help="run as daemon in the background") |
---|
| 510 | parser.add_option("-P", "--pidfile",dest="pidfile",default="/var/run/ajaxterm.pid",help="set the pidfile (default: /var/run/ajaxterm.pid)") |
---|
| 511 | parser.add_option("-i", "--index", dest="index_file", default="ajaxterm.html",help="default index file (default: ajaxterm.html)") |
---|
| 512 | parser.add_option("-u", "--uid", dest="uid", help="Set the daemon's user id") |
---|
| 513 | (o, a) = parser.parse_args() |
---|
| 514 | if o.daemon: |
---|
| 515 | pid=os.fork() |
---|
| 516 | if pid == 0: |
---|
| 517 | #os.setsid() ? |
---|
| 518 | os.setpgrp() |
---|
| 519 | nullin = file('/dev/null', 'r') |
---|
| 520 | nullout = file('/dev/null', 'w') |
---|
| 521 | os.dup2(nullin.fileno(), sys.stdin.fileno()) |
---|
| 522 | os.dup2(nullout.fileno(), sys.stdout.fileno()) |
---|
| 523 | os.dup2(nullout.fileno(), sys.stderr.fileno()) |
---|
| 524 | if os.getuid()==0 and o.uid: |
---|
| 525 | try: |
---|
| 526 | os.setuid(int(o.uid)) |
---|
| 527 | except: |
---|
| 528 | os.setuid(pwd.getpwnam(o.uid).pw_uid) |
---|
| 529 | else: |
---|
| 530 | try: |
---|
| 531 | file(o.pidfile,'w+').write(str(pid)+'\n') |
---|
| 532 | except: |
---|
| 533 | pass |
---|
| 534 | print 'AjaxTerm at http://localhost:%s/ pid: %d' % (o.port,pid) |
---|
| 535 | sys.exit(0) |
---|
| 536 | else: |
---|
| 537 | print 'AjaxTerm at http://localhost:%s/' % o.port |
---|
| 538 | at=AjaxTerm(o.cmd,o.index_file) |
---|
| 539 | # f=lambda:os.system('firefox http://localhost:%s/&'%o.port) |
---|
| 540 | # qweb.qweb_wsgi_autorun(at,ip='localhost',port=int(o.port),threaded=0,log=o.log,callback_ready=None) |
---|
| 541 | try: |
---|
| 542 | qweb.QWebWSGIServer(at,ip='localhost',port=int(o.port),threaded=0,log=o.log).serve_forever() |
---|
| 543 | except KeyboardInterrupt,e: |
---|
| 544 | sys.excepthook(*sys.exc_info()) |
---|
| 545 | at.multi.die() |
---|
| 546 | |
---|
| 547 | if __name__ == '__main__': |
---|
| 548 | main() |
---|
| 549 | |
---|