⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phprpcserver.java

📁 java 实现 phprpc 服务器端,个人认为比较好.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public void start() throws UnsupportedEncodingException {
        start(false, "UTF-8");
    }

    public void start(String charset) throws UnsupportedEncodingException {
        start(false, charset);
    }

    public void start(boolean debug) throws UnsupportedEncodingException {
        start(debug, "UTF-8");
    }

    public void start(boolean debug, String charset) throws UnsupportedEncodingException {
        this.debug = debug;
        this.charset = charset;
        this.rfs = (RemoteFunction[]) this.functions.toArray(
                new RemoteFunction[0]);
        if (this.request.getParameter("phprpc_encode") != null) {
            this.encode = !this.request.getParameter("phprpc_encode").toLowerCase().equals(
                    "false");
        }
        if (this.request.getParameter("phprpc_callback") != null) {
            this.callback = new String(
                    Base64.decode(this.request.getParameter("phprpc_callback")),
                    this.charset);
        }
        if (this.request.getParameter("phprpc_ref") != null) {
            this.byref = !this.request.getParameter("phprpc_ref").toLowerCase().equals(
                    "false");
        }
        if (this.request.getParameter("phprpc_encrypt") != null) {
            String encrypt = this.request.getParameter("phprpc_encrypt").toLowerCase();

            if (encrypt.equals("true")) {
                this.encrypt = true;
            } else if (encrypt.equals("false")) {
                this.encrypt = false;
            } else if (encrypt.equals("0")) {
                this.encryptmode = 0;
            } else if (encrypt.equals("1")) {
                this.encryptmode = 1;
            } else if (encrypt.equals("2")) {
                this.encryptmode = 2;
            }
        }
        if (this.session.getAttribute("phprpc_encrypt") != null) {
            HashMap kp = (HashMap) (this.session.getAttribute("phprpc_encrypt"));

            if (kp.containsKey("k")) {
                this.key = new byte[16];
                byte[] key = new BigInteger((String) (kp.get("k"))).toByteArray();

                for (int i = 1, n = Math.min(key.length, 16); i <= n; i++) {
                    this.key[16 - i] = key[key.length - i];
                }
            }
        }
        try {
            this.appendHeader();
            if (this.request.getParameter("phprpc_func") != null) {
                RemoteFunction[] rf = this.getFunction(
                        this.request.getParameter("phprpc_func"));

                if (rf.length > 0) {
                    byte[] arguments;

                    arguments = PHPSerializer.serialize(new Object[] {},
                            this.charset);
                    if (this.request.getParameter("phprpc_args") != null) {
                        arguments = Base64.decode(
                                this.request.getParameter("phprpc_args"));
                        if (this.encryptmode > 0) {
                            if (this.key != null) {
                                arguments = XXTEA.decrypt(arguments, this.key);
                            } else {
                                this.errno = 1;
                                this.errstr = "Can't find the key for decryption.";
                                this.writeError();
                                return;
                            }
                        }
                    }
                    Object[] args = null;
                    byte[] result = null;

                    for (int i = 0, n = rf.length; i < n; i++) {
                        for (int j = 0, m = rf[i].functions.length; j < m; j++) {
                            try {
                                args = (Object[]) PHPSerializer.unserialize(
                                        arguments, (new Object[0]).getClass(),
                                        this.charset);
                                result = call(rf[i].functions[j], rf[i].obj,
                                        args);
                                break;
                            } catch (Exception e) {
                                if (i == n - 1 && j == m - 1) {
                                    throw e;
                                }
                            }
                        }
                    }
                    if (this.byref && args != null) {
                        arguments = PHPSerializer.serialize(args, this.charset);
                    }
                    if (this.encryptmode > 0) {
                        if (this.key != null) {
                            if (this.encryptmode > 1) {
                                result = XXTEA.encrypt(result, this.key);
                            }
                            if (this.byref) {
                                arguments = XXTEA.encrypt(arguments, this.key);
                            }
                        } else {
                            this.errno = 1;
                            this.errstr = "Can't find the key for encryption.";
                            this.writeError();
                            return;
                        }
                    }
                    if (this.encode) {
                        this.result = Base64.encode(result);
                        if (this.byref) {
                            this.arguments = Base64.encode(arguments);
                        }
                    } else {
                        this.result = this.addJsSlashes(result);
                        if (this.byref) {
                            this.arguments = this.addJsSlashes(arguments);
                        }
                    }
                } else {
                    this.errno = 1;
                    this.errstr = "Can't find this function "
                            + this.request.getParameter("phprpc_func") + "().";
                }
                if (this.errno == 0) {
                    this.reswriter.print(
                            "phprpc_result=\"" + this.result + "\";\r\n");
                    if (this.byref) {
                        this.reswriter.print(
                                "phprpc_args=\"" + this.arguments + "\";\r\n");
                    }
                }
                this.writeError();
            } else {
                byte[] encrypt = null;

                if (this.encrypt) {
                    HashMap kp = KeyPairGen.genRandomKeyPair();
                    BigInteger p = new BigInteger((String) kp.get("p"));
                    BigInteger g = new BigInteger((String) kp.get("g"));
                    BigInteger x = new BigInteger(127, new Random());
                    BigInteger y = g.modPow(x, p);

                    kp.put("y", y.toString());
                    encrypt = PHPSerializer.serialize(kp, this.charset);
                    kp.put("x", x.toString());
                    this.session.setAttribute("phprpc_encrypt", kp);
                } else if (this.request.getParameter("phprpc_encrypt") != null
                        && !this.request.getParameter("phprpc_encrypt").toLowerCase().equals(
                                "false")) {
                    HashMap kp = (HashMap) (this.session.getAttribute(
                            "phprpc_encrypt"));

                    kp.put("y", this.request.getParameter("phprpc_encrypt"));
                    BigInteger y = new BigInteger((String) (kp.get("y")));
                    BigInteger x = new BigInteger((String) (kp.get("x")));
                    BigInteger p = new BigInteger((String) (kp.get("p")));
                    BigInteger k = y.modPow(x, p);

                    kp.put("k", k.toString());
                    this.session.setAttribute("phprpc_encrypt", kp);
                    encrypt = PHPSerializer.serialize(new Boolean(true), this.charset);
                }
                if (encrypt != null) {
                    if (this.encode) {
                        this.reswriter.print(
                                "phprpc_encrypt=\"" + Base64.encode(encrypt)
                                + "\";\r\n");
                    } else {
                        this.reswriter.print(
                                "phprpc_encrypt=\"" + this.addJsSlashes(encrypt)
                                + "\";\r\n");
                    }
                }
                String[] fns = new String[this.rfs.length];

                for (int i = 0, n = fns.length; i < n; i++) {
                    fns[i] = this.rfs[i].name;
                }
                if (this.encode) {
                    this.reswriter.print(
                            "phprpc_functions=\""
                                    + Base64.encode(
                                            PHPSerializer.serialize(fns,
                                            this.charset))
                                            + "\";\r\n");
                } else {
                    this.reswriter.print(
                            "phprpc_functions=\""
                                    + this.addJsSlashes(
                                            PHPSerializer.serialize(fns,
                                            this.charset))
                                            + "\";\r\n");
                }
                this.reswriter.print(this.callback);
            }
        } catch (Exception e) {
            this.errno = 1;
            if (this.debug) {
                this.errstr = e.toString();
            } else {
                this.errstr = e.getMessage();
            }
            this.writeError();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -