| | @@ -20,10 +20,11 @@ |
| 20 | 20 | ** Fossil is run as a service. |
| 21 | 21 | */ |
| 22 | 22 | #include "config.h" |
| 23 | 23 | #include "robot.h" |
| 24 | 24 | #include <assert.h> |
| 25 | +#include <time.h> |
| 25 | 26 | |
| 26 | 27 | /* |
| 27 | 28 | ** SETTING: robot-squelch width=10 default=200 |
| 28 | 29 | ** The VALUE of is an integer between 0 and 1000 that determines how |
| 29 | 30 | ** readily Fossil will squelch requests from robots. A value of 0 |
| | @@ -32,42 +33,63 @@ |
| 32 | 33 | ** and less than 1000, the decision to squelch is based on a variety |
| 33 | 34 | ** of heuristics, but is more likely to occur the larger the number. |
| 34 | 35 | */ |
| 35 | 36 | |
| 36 | 37 | /* |
| 37 | | -** Rewrite the current page with a robot squelch captcha. |
| 38 | +** Rewrite the current page with a robot squelch captcha and return 1. |
| 39 | +** |
| 40 | +** Or, if valid proof-of-work is present as either a query parameter or |
| 41 | +** as a cookie, then return 0. |
| 38 | 42 | */ |
| 39 | | -static int robot_send_captcha(void){ |
| 40 | | - unsigned h = 0; |
| 43 | +static int robot_proofofwork(void){ |
| 44 | + sqlite3_int64 tm; |
| 45 | + unsigned h1, h2; |
| 46 | + int k; |
| 41 | 47 | const char *z; |
| 48 | + const char *az[2]; |
| 42 | 49 | |
| 43 | 50 | /* Construct a proof-of-work value based on the IP address of the |
| 44 | | - ** sender and the sender's user-agent string. */ |
| 45 | | - z = P("REMOTE_ADDR"); |
| 46 | | - if( z ){ |
| 47 | | - while( *z ){ h = (h + *(unsigned char*)(z++))*0x9e3779b1; } |
| 48 | | - } |
| 49 | | - z = P("HTTP_USER_AGENT"); |
| 50 | | - if( z ){ |
| 51 | | - while( *z ){ h = (h + *(unsigned char*)(z++))*0x9e3779b1; } |
| 52 | | - } |
| 53 | | - h %= 1000000000; |
| 51 | + ** sender and the sender's user-agent string. The current time also |
| 52 | + ** affects the pow value, so actually compute two values, one for the |
| 53 | + ** current 900-second interval and one for the previous. Either can |
| 54 | + ** match. The pow-value is an integer between 100,000,000 and |
| 55 | + ** 999,999,999. */ |
| 56 | + az[0] = P("REMOTE_ADDR"); |
| 57 | + az[1] = P("HTTP_USER_AGENT"); |
| 58 | + tm = time(0); |
| 59 | + h1 = (unsigned)((tm&0xffffffff) / 900); |
| 60 | + h2 = h1 - 1; |
| 61 | + for(k=0; k<2; k++){ |
| 62 | + z = az[k]; |
| 63 | + if( z==0 ) continue; |
| 64 | + while( *z ){ |
| 65 | + h1 = (h1 + *(unsigned char*)z)*0x9e3779b1; |
| 66 | + h2 = (h2 + *(unsigned char*)z)*0x9e3779b1; |
| 67 | + z++; |
| 68 | + } |
| 69 | + } |
| 70 | + h1 = (h1 % 900000000) + 100000000; |
| 71 | + h2 = (h2 % 900000000) + 100000000; |
| 54 | 72 | |
| 55 | 73 | /* If there is already a proof-of-work cookie with this value |
| 56 | 74 | ** that means that the user agent has already authenticated. |
| 57 | 75 | */ |
| 58 | 76 | z = P("fossil-proofofwork"); |
| 59 | | - if( z && atoi(z)==h ){ |
| 77 | + if( z |
| 78 | + && (atoi(z)==h1 || atoi(z)==h2) |
| 79 | + && !cgi_is_qp("fossil-proofofwork") ){ |
| 60 | 80 | return 0; |
| 61 | 81 | } |
| 62 | 82 | |
| 63 | 83 | /* Check for a proof query parameter. If found, that means that |
| 64 | 84 | ** the captcha has just now passed, so set the proof-of-work cookie |
| 65 | 85 | ** in addition to letting the request through. |
| 66 | 86 | */ |
| 67 | 87 | z = P("proof"); |
| 68 | | - if( z && atoi(z)==h ){ |
| 88 | + if( z |
| 89 | + && (atoi(z)==h1 || atoi(z)==h2) |
| 90 | + ){ |
| 69 | 91 | cgi_set_cookie("fossil-proofofwork",z,"/",900); |
| 70 | 92 | return 0; |
| 71 | 93 | } |
| 72 | 94 | cgi_tag_query_parameter("proof"); |
| 73 | 95 | |
| | @@ -81,16 +103,22 @@ |
| 81 | 103 | cgi_query_parameters_to_hidden(); |
| 82 | 104 | @ <input id="vx" type="hidden" name="proof" value="0"> |
| 83 | 105 | @ <input id="cx" type="submit" value="Wait..." disabled> |
| 84 | 106 | @ </form> |
| 85 | 107 | @ <script nonce='%s(style_nonce())'> |
| 86 | | - @ function enableHuman(){ |
| 87 | | - @ document.getElementById("vx").value = %u(h); |
| 88 | | - @ document.getElementById("cx").value = "Ok"; |
| 89 | | - @ document.getElementById("cx").disabled = false; |
| 108 | + @ function Nhtot1520(x){return document.getElementById(x);} |
| 109 | + @ function Aoxlxzajv(h){\ |
| 110 | + @ Nhtot1520("vx").value=h;\ |
| 111 | + @ Nhtot1520("cx").value="Ok";\ |
| 112 | + @ Nhtot1520("cx").disabled=false;\ |
| 113 | + @ } |
| 114 | + @ function Vhcnyarsm(h,a){\ |
| 115 | + @ if(a>0){setTimeout(Vhcnyarsm,1,h+a,a-1);}else{Aoxlxzajv(h);}\ |
| 90 | 116 | @ } |
| 91 | | - @ setTimeout(function(){enableHuman();}, 500); |
| 117 | + k = 200 + h2%99; |
| 118 | + h2 = (k*k + k)/2; |
| 119 | + @ setTimeout(function(){Vhcnyarsm(%u(h1-h2),%u(k));},10); |
| 92 | 120 | @ </script> |
| 93 | 121 | style_finish_page(); |
| 94 | 122 | return 1; |
| 95 | 123 | } |
| 96 | 124 | |
| | @@ -97,13 +125,16 @@ |
| 97 | 125 | |
| 98 | 126 | /* |
| 99 | 127 | ** WEBPAGE functions can invoke this routine with an argument |
| 100 | 128 | ** that is between 0 and 1000. Based on that argument, and on |
| 101 | 129 | ** other factors, this routine decides whether or not to squelch |
| 102 | | -** the request. "Squelch" in this context, means paint a captcha |
| 103 | | -** rather than complete the original request. The idea here is to |
| 104 | | -** prevent server overload due to excess robot traffic. |
| 130 | +** the request. "Squelch" in this context, means to require the |
| 131 | +** client to show proof-of-work before the request is processed. |
| 132 | +** The idea here is to prevent server overload due to excess robot |
| 133 | +** traffic. If a robot (or any client application really) wants us |
| 134 | +** to spend a lot of CPU computing some result for it, then it needs |
| 135 | +** to first demonstrate good faith by doing some make-work for us. |
| 105 | 136 | ** |
| 106 | 137 | ** This routine returns true for a squelch and false if the original |
| 107 | 138 | ** request should go through. |
| 108 | 139 | ** |
| 109 | 140 | ** The input parameter is an estimate of how much CPU time |
| | @@ -128,10 +159,10 @@ |
| 128 | 159 | ){ |
| 129 | 160 | return 0; /* There is a valid token= query parameter */ |
| 130 | 161 | } |
| 131 | 162 | iSquelch = db_get_int("robot-squelch",200); |
| 132 | 163 | if( iSquelch<=0 ) return 0; |
| 133 | | - if( n+iSquelch>=1000 && robot_send_captcha() ){ |
| 164 | + if( n+iSquelch>=1000 && robot_proofofwork() ){ |
| 134 | 165 | return 1; |
| 135 | 166 | } |
| 136 | 167 | return 0; |
| 137 | 168 | } |
| 138 | 169 | |