~/paste/1164
~/paste/1164
~/paste/1164

  1. <html>
  2. <head>
  3. <title>jQuery POST test</title>
  4. <script type="text/javascript" src="./jquery-1.7.2.js"></script>
  5. <script type="text/javascript" src="./jquery.cookie.js"></script>
  6.  
  7. <script type="text/javascript">
  8.   var host    = "127.0.0.1"           // hostname / IP address of machine running OpenWrt with luci RPC support
  9.   var port    = "8080"                // port of webserver running luci RPC service
  10.   var proto   = "http"                // protocol (e.g. http, https, etc.)
  11.   var path    = "/cgi-bin/luci/rpc/"  // path to luci RPC interface
  12.   var url     = proto + "://" + host + ":" + port + path
  13.   console.debug(url);
  14.  
  15.   var anonSections = new Array(); // this array is used to keep track of hashes for anonymous sections
  16.                                   // (example 'get_all' demonstrates its usage)
  17.  
  18.   // generic luci RPC function
  19.   // @submodule:    luci submodule (e.g. "auth", "fs", "uci", etc.)
  20.   // @method:       RPC method (provided respective submodule)
  21.   // @data:         RPC function arguments in JSON notation
  22.   // @callOnSuc:    callback function which gets call when JSON-RPC was successfull
  23.   // @callOnErr:    callback function which gets called when JSON-RPC failed
  24.   function luci_rpc(submodule, method, data, callOnSuc, callOnErr) {
  25.     console.debug("Info: " + data);
  26.     console.debug("luci_rpc called:\n  on:   " + url + submodule + "?auth=" + $.cookie('sysauth', {path: '/'}) + "\n  with: " + JSON.stringify ({method:method, params:data}));
  27.     $.ajax({
  28.       // Fetch auth-token from cookie and append it to URL.
  29.       // That doesn't really make sense, since appending the token
  30.       // is supposed to be an alternative to clients _without_ cookie
  31.       // support - however just sending the cookie (without having
  32.       // the token within the URL) doesn't seem to be sufficient.
  33.       // I consider this a bug. Since we can't store the token
  34.       // somewhere else but in a cookie we now require cookie
  35.       // support _and_ append the token to the URL to get
  36.       // authentication working
  37.       //url: url + submodule + "?auth=" + $.cookie('sysauth'),
  38.       url: url + submodule + ($.cookie('sysauth', {path: '/'}) ? "?auth=" + $.cookie('sysauth', {path: '/'}) : ""),
  39.       data: JSON.stringify ({method:method, params:data}),
  40.       type: "POST",
  41.       dataType: "json",
  42.       success: function (res, status, jqXHR) {
  43.         if (callOnSuc)
  44.           console.debug("RPC was successfull");
  45.           callOnSuc(res, status, jqXHR);
  46.       },
  47.       error: function (err, status, excp) {
  48.         if (callOnErr)
  49.           console.debug("RPC failed");
  50.           callOnErr(err, status, excp);
  51.       }
  52.     })
  53.   }
  54.  
  55.   //function setCookie(token) {
  56.   //  //$.cookie('openrouter', token,  { path: '/', expires: 10 });
  57.   //  $.cookie('authToken', token,  { path: '/', expires: 10 });
  58.   //}
  59.  
  60.   function deleteCookie() {
  61.     $.cookie('sysauth', null, {path: '/'});
  62.   }
  63.  
  64.   //function showCookiee() {
  65.   //  alert($.cookie('syscookie'));
  66.   //}
  67.  
  68.   function logout() {
  69.     deleteCookie();
  70.   }
  71.  
  72.   // submodule: auth
  73.   function login(pass) {
  74.     luci_rpc("auth", "login", ["root", pass],
  75.       // callback for 'login' if RPC succeded
  76.       function(res, status, jqXHR) {
  77.         // yay, we got an authentication token!
  78.         // cookie named 'sysauth' will be set automatically when auth was successfull
  79.         if (res.result) {
  80.           alert("Authentication successful! Received auth-token: " + res.result);
  81.           return;
  82.         // no authentication token for us.. -> invalid credentials
  83.         } else {
  84.           alert("Authentication failed!");
  85.           return;
  86.         }
  87.       },
  88.       // callback for 'login' if RPC failed
  89.       function(res, status, excp) {
  90.         alert("JSON-RPC query failed!\nError: " + res + "\nStatus: " + status+ "\nException:" + excp);
  91.         return;
  92.       }
  93.     )
  94.   }
  95.  
  96.   // submodule: sys
  97.   function getHostname() {
  98.     luci_rpc("sys", "hostname", null,
  99.       function(res) { document.getElementById('hostname').value = res.result },
  100.       function() { document.getElementById('hostname').value = "NO DATA (logged in?)" }
  101.     );
  102.   }
  103.  
  104.   function setHostname(hostname) {
  105.     luci_rpc("sys", "hostname", [hostname],
  106.      function(res) { console.debug("success"); alert("Successfully changed hostname") },
  107.      function() { console.debug("error"); alert("Failed! Maybe not logged in?") }
  108.     );
  109.   }
  110.  
  111.   // submodule: uci
  112.   //    function: get
  113.   function getProto() {
  114.     luci_rpc("uci", "get", ["network", "lan", "proto"],
  115.       function(res) { document.getElementById('proto').value = res.result },
  116.       function() { document.getElementById('proto').value = "NO DATA (logged in?)" }
  117.     );
  118.   }
  119.  
  120.   //    function: get_all
  121.   function getSSHPort() {
  122.     // In UCI you can use unnamed (=anonymous) sections, however the JSON RPC API
  123.     // doesn't provide RPCs to access particular sections without knowing its hash.
  124.     // That's why we have to fetch the whole config and iterate over the unnamed
  125.     // sections afterwards. In this very case, we just want to access the first unnamed
  126.     // section, since that's supposed to be the only one anyway.
  127.     luci_rpc("uci", "get_all", ["dropbear"],
  128.       function(res) {
  129.         anonSections['dropbear'] = new Array();
  130.         for (var prop in res.result) {
  131.           anonSections['dropbear'][0] = prop;
  132.           document.getElementById('sshPort').value = res.result[anonSections.dropbear[0]].Port
  133.           break;
  134.         }
  135.       },
  136.       function() { document.getElementById('sshPort').value = "NO DATA (logged in?)" }
  137.     );
  138.   }
  139.  
  140.   //    function: set
  141.   function setSSHPort(sshPort) {
  142.     luci_rpc("uci", "set", ["dropbear", anonSections['dropbear'][0], "Port", sshPort],
  143.       function(res) { console.debug("success"); alert("Successfully changed SSH port") },
  144.       function() { console.debug("error"); alert("Failed! Maybe not logged in?") }
  145.     );
  146.     luci_rpc("uci", "apply", ["dropbear"]);
  147.     luci_rpc("uci", "commit", ["dropbear"]);
  148.   }
  149.  
  150.   $(document).ready(function() {
  151.     // execute code when page is accessed / loaded
  152.     getHostname();
  153.     getProto();
  154.     getSSHPort();
  155.  
  156.     // call functions when respective events got triggered (e.g. button got pressed)
  157.     $("#login").click(function() {
  158.       login(document.getElementById('pass').value);
  159.     });
  160.     $("#logout").click(function() {
  161.       logout();
  162.     });
  163.     $("#setHostname").click(function() {
  164.       setHostname(document.getElementById('hostname').value);
  165.     });
  166.     $("#setSSHPort").click(function() {
  167.       setSSHPort(document.getElementById('sshPort').value);
  168.     });
  169.   });
  170. </script>
  171. </head>
  172. <body>
  173. <h2> Package: auth </h2>
  174. <h3> Method: login </h3>
  175. <p>
  176. Credential: <input type="text" id="pass" />
  177. <button id="login">Login</button>
  178. <button id="logout">Logout</button>
  179. </p>
  180. <h2> Package: sys </h2>
  181. <h3> Method: hostname </h3>
  182. <p>
  183. Hostname: <input type="text" id="hostname" />
  184. <button id="setHostname">Change hostname</button>
  185. </p>
  186. <h2> Package: uci </h2>
  187. <h3> Method: get </h3>
  188. <p>
  189. Protocol used on LAN port: <input type="text" id="proto" readonly />
  190. </p>
  191. <h3> Method: set </h3>
  192. <p>
  193. Port SSH daemon is running on: <input type="text" id="sshPort" />
  194. <button id="setSSHPort">Change SSH port</button>
  195. </p>
  196.  
  197. </body>
  198. </html>
  199.  
Language: javascript
Posted by Anonymous at 28 Jun 2012, 02:42:35 UTC