Browse Source

Enhancements

Fix compiler warnings.
Add detailed description for some settings in pref configuration.
Add passing arguments of alias name to alias target.
Optimize codes.
Tindy X 5 years ago
parent
commit
8662aa36cc
9 changed files with 117 additions and 104 deletions
  1. 15 1
      base/pref-new.yml
  2. 22 1
      base/pref.ini
  3. 0 70
      src/main.cpp
  4. 2 0
      src/misc.cpp
  5. 1 1
      src/subexport.cpp
  6. 2 2
      src/upload.cpp
  7. 59 20
      src/webget.cpp
  8. 8 8
      src/webget.h
  9. 8 1
      src/webserver_libevent.cpp

+ 15 - 1
base/pref-new.yml

@@ -81,7 +81,7 @@ proxy_group:
   - {import: snippets/groups.txt}
 
 template:
-  template_path: "template"
+  template_path: "templates"
   globals:
   - {key: clash.http_port, value: 7890}
   - {key: clash.socks_port, value: 7891}
@@ -90,6 +90,20 @@ template:
   
 aliases:
   - {uri: /v, target: /version}
+  - {uri: /clash, target: "/sub?target=clash"}
+  - {uri: /clashr, target: "/sub?target=clashr"}
+  - {uri: /surge, target: "/sub?target=surge"}
+  - {uri: /quan, target: "/sub?target=quan"}
+  - {uri: /quanx, target: "/sub?target=quanx"}
+  - {uri: /mellow, target: "/sub?target=mellow"}
+  - {uri: /surfboard, target: "/sub?target=surfboard"}
+  - {uri: /loon, target: "/sub?target=loon"}
+  - {uri: /ss, target: "/sub?target=ss"}
+  - {uri: /ssd, target: "/sub?target=ssd"}
+  - {uri: /sssub, target: "/sub?target=sssub"}
+  - {uri: /ssr, target: "/sub?target=ssr"}
+  - {uri: /v2ray, target: "/sub?target=v2ray"}
+  - {uri: /trojan, target: "/sub?target=trojan"}
 
 server:
   listen: 0.0.0.0

+ 22 - 1
base/pref.ini

@@ -168,14 +168,35 @@ surge_ruleset=!!import:snippets/rulesets.txt
 custom_proxy_group=!!import:snippets/groups.txt
 
 [template]
-template_path=template
+;The file scope limit of 'include' statement inside the templates.
+template_path=templates
+
+;The following settings will be added to the "global" scope of the template variables
+;Value of 'clash.http_port' can be accessed with 'global.clash.http_port' in the template.
 clash.http_port=7890
 clash.socks_port=7891
 clash.allow_lan=true
 clash.log_level=info
 
 [aliases]
+;Aliases for accessing interfaces. Can be used to shorten the URI.
+;All arguments passed when accessing the alias name will be appended to the arguments of the alias target.
+;Format: uri=target
 /v=/version
+/clash=/sub?target=clash
+/clashr=/sub?target=clashr
+/surge=/sub?target=surge
+/quan=/sub?target=quan
+/quanx=/sub?target=quanx
+/mellow=/sub?target=mellow
+/surfboard=/sub?target=surfboard
+/loon=/sub?target=loon
+/ss=/sub?target=ss
+/ssd=/sub?target=ssd
+/sssub=/sub?target=sssub
+/ssr=/sub?target=ssr
+/v2ray=/sub?target=v2ray
+/trojan=/sub?target=trojan
 
 [server]
 ;Address to bind on for Web Server

+ 0 - 70
src/main.cpp

@@ -213,76 +213,6 @@ int main(int argc, char *argv[])
 
     append_response("GET", "/qx-rewrite", "text/plain;charset=utf-8", getRewriteRemote);
 
-    append_response("GET", "/clash", "text/plain;charset=utf-8", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=clash", postdata, status_code, extra_headers);
-    });
-
-    append_response("GET", "/clashr", "text/plain;charset=utf-8", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=clashr", postdata, status_code, extra_headers);
-    });
-
-    append_response("GET", "/surge", "text/plain;charset=utf-8", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=surge", postdata, status_code, extra_headers);
-    });
-
-    append_response("GET", "/surfboard", "text/plain;charset=utf-8", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=surfboard", postdata, status_code, extra_headers);
-    });
-
-    append_response("GET", "/mellow", "text/plain;charset=utf-8", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=mellow", postdata, status_code, extra_headers);
-    });
-
-    append_response("GET", "/ss", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=ss", postdata, status_code, extra_headers);
-    });
-
-    append_response("GET", "/sssub", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=sssub", postdata, status_code, extra_headers);
-    });
-
-    append_response("GET", "/ssr", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=ssr", postdata, status_code, extra_headers);
-    });
-
-    append_response("GET", "/v2ray", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=v2ray", postdata, status_code, extra_headers);
-    });
-
-    append_response("GET", "/quan", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=quan", postdata, status_code, extra_headers);
-    });
-
-    append_response("GET", "/quanx", "text/plain;charset=utf-8", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=quanx", postdata, status_code, extra_headers);
-    });
-
-    append_response("GET", "/loon", "text/plain;charset=utf-8", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=loon", postdata, status_code, extra_headers);
-    });
-
-    append_response("GET", "/ssd", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=ssd", postdata, status_code, extra_headers);
-    });
-
-    append_response("GET", "/trojan", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
-    {
-        return subconverter(argument + "&target=trojan", postdata, status_code, extra_headers);
-    });
-
     append_response("GET", "/render", "text/plain;charset=utf-8", renderTemplate);
 
     if(!api_mode)

+ 2 - 0
src/misc.cpp

@@ -189,10 +189,12 @@ std::string UrlDecode(const std::string& str)
     return strTemp;
 }
 
+/*
 static inline bool is_base64(unsigned char c)
 {
     return (isalnum(c) || (c == '+') || (c == '/'));
 }
+*/
 
 std::string base64_encode(const std::string &string_to_encode)
 {

+ 1 - 1
src/subexport.cpp

@@ -2160,7 +2160,7 @@ void netchToQuanX(std::vector<nodeInfo> &nodes, INIReader &ini, std::vector<rule
             proxies = vArray[0] + ",";
             proxies += std::accumulate(vArray.begin() + 3, vArray.end(), vArray[2], [](std::string a, std::string b)
             {
-                return std::move(a) + "," + std::move(replace_all_distinct(b, "=", ":"));
+                return std::move(a) + "," + replace_all_distinct(b, "=", ":");
             });
             ini.Set("{NONAME}", vArray[1] + "=" + proxies); //insert order
             continue;

+ 2 - 2
src/upload.cpp

@@ -73,7 +73,7 @@ int uploadGist(std::string name, std::string path, std::string content, bool wri
     {
         //std::cerr<<"No gist id is provided. Creating new gist...\n";
         writeLog(0, "No Gist id is provided. Creating new Gist...", LOG_LEVEL_ERROR);
-        retVal = webPost("https://api.github.com/gists", buildGistData(path, content), getSystemProxy(), token, &retData);
+        retVal = webPost("https://api.github.com/gists", buildGistData(path, content), getSystemProxy(), {"Authorization: token " + token}, &retData);
         if(retVal != 201)
         {
             //std::cerr<<"Create new Gist failed! Return data:\n"<<retData<<"\n";
@@ -88,7 +88,7 @@ int uploadGist(std::string name, std::string path, std::string content, bool wri
         writeLog(0, "Gist id provided. Modifying Gist...", LOG_LEVEL_INFO);
         if(writeManageURL)
             content = "#!MANAGED-CONFIG " + url + "\n" + content;
-        retVal = webPatch("https://api.github.com/gists/" + id, buildGistData(path, content), getSystemProxy(), token, &retData);
+        retVal = webPatch("https://api.github.com/gists/" + id, buildGistData(path, content), getSystemProxy(), {"Authorization: token " + token}, &retData);
         if(retVal != 200)
         {
             //std::cerr<<"Modify gist failed! Return data:\n"<<retData<<"\n";

+ 59 - 20
src/webget.cpp

@@ -58,7 +58,7 @@ static inline void curl_set_common_options(CURL *curl_handle, const char *url)
     curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, user_agent_str.data());
 }
 
-static std::string curlGet(std::string url, std::string proxy, std::string &response_headers, CURLcode &return_code)
+static std::string curlGet(const std::string &url, const std::string &proxy, std::string &response_headers, CURLcode &return_code)
 {
     CURL *curl_handle;
     std::string data;
@@ -72,7 +72,7 @@ static std::string curlGet(std::string url, std::string proxy, std::string &resp
     curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &data);
     curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, writer);
     curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, &response_headers);
-    if(proxy != "")
+    if(proxy.size())
         curl_easy_setopt(curl_handle, CURLOPT_PROXY, proxy.data());
 
     return_code = curl_easy_perform(curl_handle);
@@ -87,7 +87,7 @@ static std::string curlGet(std::string url, std::string proxy, std::string &resp
 }
 
 // data:[<mediatype>][;base64],<data>
-static std::string dataGet(std::string url)
+static std::string dataGet(const std::string &url)
 {
     if (!startsWith(url, "data:"))
         return "";
@@ -103,14 +103,14 @@ static std::string dataGet(std::string url)
     }
 }
 
-std::string buildSocks5ProxyString(std::string addr, int port, std::string username, std::string password)
+std::string buildSocks5ProxyString(const std::string &addr, int port, const std::string &username, const std::string &password)
 {
-    std::string authstr = username != "" && password != "" ? username + ":" + password + "@" : "";
+    std::string authstr = username.size() && password.size() ? username + ":" + password + "@" : "";
     std::string proxystr = "socks5://" + authstr + addr + ":" + std::to_string(port);
     return proxystr;
 }
 
-std::string webGet(std::string url, std::string proxy, std::string &response_headers, unsigned int cache_ttl)
+std::string webGet(const std::string &url, const std::string &proxy, std::string &response_headers, unsigned int cache_ttl)
 {
     std::string content;
     CURLcode return_code;
@@ -161,19 +161,19 @@ std::string webGet(std::string url, std::string proxy, std::string &response_hea
     return curlGet(url, proxy, response_headers, return_code);
 }
 
-std::string webGet(std::string url, std::string proxy)
+std::string webGet(const std::string &url, const std::string &proxy)
 {
     std::string dummy;
     return webGet(url, proxy, dummy);
 }
 
-std::string webGet(std::string url, std::string proxy, unsigned int cache_ttl)
+std::string webGet(const std::string &url, const std::string &proxy, unsigned int cache_ttl)
 {
     std::string dummy;
     return webGet(url, proxy, dummy, cache_ttl);
 }
 
-int curlPost(std::string url, std::string data, std::string proxy, std::string auth_token, std::string *retData)
+int curlPost(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
 {
     CURL *curl_handle;
     CURLcode res;
@@ -183,8 +183,8 @@ int curlPost(std::string url, std::string data, std::string proxy, std::string a
     curl_init();
     curl_handle = curl_easy_init();
     list = curl_slist_append(list, "Content-Type: application/json;charset='utf-8'");
-    if(auth_token.size())
-        list = curl_slist_append(list, std::string("Authorization: token " + auth_token).data());
+    for(const std::string &x : request_headers)
+        list = curl_slist_append(list, x.data());
 
     curl_set_common_options(curl_handle, url.data());
     curl_easy_setopt(curl_handle, CURLOPT_POST, 1L);
@@ -194,7 +194,7 @@ int curlPost(std::string url, std::string data, std::string proxy, std::string a
     curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, retData);
     curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);
     curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, user_agent_str.data());
-    if(proxy != "")
+    if(proxy.size())
         curl_easy_setopt(curl_handle, CURLOPT_PROXY, proxy.data());
 
     res = curl_easy_perform(curl_handle);
@@ -210,12 +210,12 @@ int curlPost(std::string url, std::string data, std::string proxy, std::string a
     return retVal;
 }
 
-int webPost(std::string url, std::string data, std::string proxy, std::string auth_token, std::string *retData)
+int webPost(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
 {
-    return curlPost(url, data, proxy, auth_token, retData);
+    return curlPost(url, data, proxy, request_headers, retData);
 }
 
-int curlPatch(std::string url, std::string data, std::string proxy, std::string auth_token, std::string *retData)
+int curlPatch(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
 {
     CURL *curl_handle;
     CURLcode res;
@@ -227,8 +227,8 @@ int curlPatch(std::string url, std::string data, std::string proxy, std::string
     curl_handle = curl_easy_init();
 
     list = curl_slist_append(list, "Content-Type: application/json;charset='utf-8'");
-    if(auth_token.size())
-        list = curl_slist_append(list, std::string("Authorization: token " + auth_token).data());
+    for(const std::string &x : request_headers)
+        list = curl_slist_append(list, x.data());
 
     curl_set_common_options(curl_handle, url.data());
     curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, "PATCH");
@@ -238,7 +238,7 @@ int curlPatch(std::string url, std::string data, std::string proxy, std::string
     curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, retData);
     curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);
     curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, user_agent_str.data());
-    if(proxy != "")
+    if(proxy.size())
         curl_easy_setopt(curl_handle, CURLOPT_PROXY, proxy.data());
 
     res = curl_easy_perform(curl_handle);
@@ -253,7 +253,46 @@ int curlPatch(std::string url, std::string data, std::string proxy, std::string
     return retVal;
 }
 
-int webPatch(std::string url, std::string data, std::string proxy, std::string auth_token, std::string *retData)
+int webPatch(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
 {
-    return curlPatch(url, data, proxy, auth_token, retData);
+    return curlPatch(url, data, proxy, request_headers, retData);
+}
+
+int curlHead(const std::string &url, const std::string &proxy, const string_array &request_headers, std::string &response_headers)
+{
+    CURL *curl_handle;
+    CURLcode res;
+    long retVal = 0;
+    struct curl_slist *list = NULL;
+
+    curl_init();
+
+    curl_handle = curl_easy_init();
+
+    list = curl_slist_append(list, "Content-Type: application/json;charset='utf-8'");
+    for(const std::string &x : request_headers)
+        list = curl_slist_append(list, x.data());
+
+    curl_set_common_options(curl_handle, url.data());
+    curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, writer);
+    curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, &response_headers);
+    curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1L);
+    curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);
+    curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, user_agent_str.data());
+    if(proxy.size())
+        curl_easy_setopt(curl_handle, CURLOPT_PROXY, proxy.data());
+
+    res = curl_easy_perform(curl_handle);
+    curl_slist_free_all(list);
+    if(res == CURLE_OK)
+        res = curl_easy_getinfo(curl_handle, CURLINFO_HTTP_CODE, &retVal);
+
+    curl_easy_cleanup(curl_handle);
+
+    return retVal;
+}
+
+int webHead(const std::string &url, const std::string &proxy, const string_array &request_headers, std::string &response_headers)
+{
+    return curlHead(url, proxy, request_headers, response_headers);
 }

+ 8 - 8
src/webget.h

@@ -9,15 +9,15 @@
 
 #include "misc.h"
 
-std::string webGet(std::string url, std::string proxy);
-std::string webGet(std::string url, std::string proxy, unsigned int cache_ttl);
-std::string webGet(std::string url, std::string proxy, std::string &response_headers, unsigned int cache_ttl = 0);
-int webPost(std::string url, std::string data, std::string proxy, std::string auth_token, std::string *retData);
-int webPatch(std::string url, std::string data, std::string proxy, std::string auth_token, std::string *retData);
-std::string buildSocks5ProxyString(std::string addr, int port, std::string username, std::string password);
+std::string webGet(const std::string &url, const std::string &proxy);
+std::string webGet(const std::string &url, const std::string &proxy, unsigned int cache_ttl);
+std::string webGet(const std::string &url, const std::string &proxy, std::string &response_headers, unsigned int cache_ttl = 0);
+int webPost(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData);
+int webPatch(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData);
+std::string buildSocks5ProxyString(const std::string &addr, int port, const std::string &username, const std::string &password);
 
 // Unimplemented: (CURLOPT_HTTPHEADER: Host:)
-std::string httpGet(std::string host, std::string addr, std::string uri);
-std::string httpsGet(std::string host, std::string addr, std::string uri);
+std::string httpGet(const std::string &host, const std::string &addr, const std::string &uri);
+std::string httpsGet(const std::string &host, const std::string &addr, const std::string &uri);
 
 #endif // WEBGET_H_INCLUDED

+ 8 - 1
src/webserver_libevent.cpp

@@ -69,10 +69,17 @@ static inline int process_request(const char *method_str, std::string uri, std::
         }
     }
 
-    auto iter = redirect_map.find(uri);
+    auto iter = redirect_map.find(path);
     if(iter != redirect_map.end())
     {
         return_data = iter->second;
+        if(arguments.size())
+        {
+            if(return_data.find("?") != return_data.npos)
+                return_data += "&" + arguments;
+            else
+                return_data += "?" + arguments;
+        }
         content_type = "REDIRECT";
         return 0;
     }