V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
openercn
V2EX  ›  程序员

三行代码实现 http shell, OPener_Server 第四弹 (更多教程更新中...)

  •  
  •   openercn · 2016-11-22 21:50:16 +08:00 · 2122 次点击
    这是一个创建于 2711 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Http shell

    同样是使用 jquey, 你已经使用了生成 helloworld 的上面那段代码生成了 helloworld ,下面你只要下面的代码就可以有一个 http shell :

    
    var start_http_server2={'action':'new_http_server','ip':'','port':'1009'}; // 开启一个新的 http 服务器,监听在端口 1009 上
    var reg_url={'action':'reg_url','type':'ajax_post','url':'/shell','host':'*:1009','go':shell}; 
    //注册一个 url 地址 /shell ,绑定到 1008 端口上,设定这个 url 处理模式为 ajax post 模式,最后处理这个 url 的代码放到 shell
    
    var shell=`my ($r,$key,$data)=@_; 
    my $rr=`$data`;
    $n->{send_resp}->($r,$key,{type=>'/shell',result=>'ok',g=>$rr});
    `;
    
    url_post_data(url,JSON.stringify(start_http_server2)); 
    url_post_data(url,JSON.stringify(reg_url));
    
    

    Shell 代码只有三行:

    my ($r,$key,$data)=@_;  ### 接收参数
    my $rr=`$data`;         ### 执行,并取得结果
    $n->{send_resp}->($r,$key,{type=>'/shell',result=>'ok',g=>$rr});  ### 通过 http 返回结果
    

    测试执行效果:

    var url2="http://test1.openerserver.com:1009/shell"; 
    url_post_data(url2,'ls');
    

    在 chrome 的 console.log 中看结果。

    附上 Hello world 教程

    用 jquery :

    
    var start_http_server={'action':'new_http_server','ip':'','port':'1008'}; // 开启一个新的 http 服务器,监听在端口 1008 上
    var reg_url={'action':'reg_url','type':'http_get','url':'/helloworld','host':'*:1008','go':hello_fun}; 
    //注册一个 url 地址 /helloworld ,绑定到 1008 端口上,设定这个 url 处理模式为 http get 模式,最后处理这个 url 的代码放到 hello_fun
    
    var hello_fun=`my ($r,$key)=@_; 
    $n->{send_normal_resp}->($r,$key,'Hello Worlds'); 
    
    `;
    //### 接收传入参数,$r 包含所有这个 http 请求相关信息,$key 包含该 http 请求的唯一 id
    //### 发送返回,返回内容是 html ,加入一个 hello world 字符串。
    
    url_post_data(url,JSON.stringify(start_http_server)); 
    url_post_data(url,JSON.stringify(reg_url));
    
    var url="https://test1.openerserver.com:10008/op";  // 安装了 opener_server 的服务器地址: test1.openerserver.com 
    var opener_flag='opener'; // 设定 http header 中 opener_flag 字段,相当于访问该 opener_server 的密码
    function url_post_data(go,data){
    	$.ajax({
    		  url: go,
    		  cache: false,
    		  headers: {
    			  opener_flag:opener_flag
    		  },
    		  data: data,
    		  type: 'POST',
    		  dataType: 'json',
    		  success: function(data){
    			if (data.result=='ok')
    			{	
            console.log(data);		
    			}else{
    				console.log('error');
    			}
    		  },
    		  error: function(dd,mm){
    			console.log('error:');
    			console.log(dd);
    			console.log(mm);
    			}
    	});
    }
    

    在一个含有 jquery 代码的 html 页面上,执行上面的代码就可以了。
    然后访问 http://test1.openervpn.com:1008/helloworld 就可以看到结果。

    OPener_Server 教程:

    第 1 弹: https://www.v2ex.com/t/320838
    第 2 弹: https://www.v2ex.com/t/321686
    第 3 弹: https://www.v2ex.com/t/322192

    项目地址: https://github.com/openerserver/openerserver_perl

    微博: http://weibo.com/openerserver

    微信公众号:

    openervpn
        1
    openervpn  
       2016-11-22 22:16:12 +08:00
    关注中....

    希望楼主再接再厉
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1233 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 23:14 · PVG 07:14 · LAX 16:14 · JFK 19:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.