diff --git a/peer.php b/peer.php index 4c8a50c..f521cb2 100755 --- a/peer.php +++ b/peer.php @@ -23,6 +23,7 @@ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +const SANITY_LOCK_PATH = __DIR__.'/tmp/sanity-lock'; require_once __DIR__.'/include/init.inc.php'; header('Content-Type: application/json'); @@ -41,6 +42,12 @@ if ($_POST['coin'] != $_config['coin']) { $ip = san_ip($_SERVER['REMOTE_ADDR']); $ip = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE); +// there should not be any local peering attempts +if($_SERVER['REMOTE_ADDR']==$_SERVER['SERVER_ADDR']){ + api_err("invalid-ip"); +} + + // peer with the current node if ($q == "peer") { // sanitize the hostname @@ -164,6 +171,14 @@ if ($q == "peer") { _log('['.$ip."] Block rejected due to sanity sync"); //api_err("sanity-sync"); } + $sanity_lock=fopen(SANITY_LOCK_PATH,'w+'); + if(!flock($sanity_lock, LOCK_EX | LOCK_NB)){ + _log('['.$ip."] Block rejected due to sanity sync"); + api_err("sanity-sync"); + } + // make sure sanity doesn't start while we add a new block and no other blocks are added at the same time + flock($sanity_lock, LOCK_EX); + $data['id'] = san($data['id']); $current = $block->current(); // block already in the blockchain diff --git a/sanity.php b/sanity.php index 78996ca..5f3ef11 100755 --- a/sanity.php +++ b/sanity.php @@ -37,29 +37,13 @@ if (php_sapi_name() !== 'cli') { require_once __DIR__.'/include/init.inc.php'; // make sure there's only a single sanity process running at the same time -if (file_exists(SANITY_LOCK_PATH)) { - $ignore_lock = false; - if ($argv[1] == "force") { - $res = intval(shell_exec("ps aux|grep sanity.php|grep -v grep|wc -l")); - if ($res == 1) { - $ignore_lock = true; - } - } - $pid_time = filemtime(SANITY_LOCK_PATH); - - // If the process died, restart after 60 times the sanity interval - if (time() - $pid_time > ($_config['sanity_interval'] * 60 ?? 900 * 60)) { - @unlink(SANITY_LOCK_PATH); - } - - if (!$ignore_lock) { - die("Sanity lock in place".PHP_EOL); - } +$sanity_lock=fopen(SANITY_LOCK_PATH,'w+'); +if(!flock($sanity_lock, LOCK_EX | LOCK_NB)){ + die("Sanity lock in place".PHP_EOL); } - // set the new sanity lock -$lock = fopen(SANITY_LOCK_PATH, "w"); -fclose($lock); +flock($sanity_lock, LOCK_EX); + $arg = trim($argv[1]); $arg2 = trim($argv[2]); echo "Sleeping for 3 seconds\n";