Пример #1
0
 public static function add($cidr)
 {
     // pr($subnet);
     DB::transaction(function () use($cidr) {
         $subnet = new self(['subnet' => $cidr]);
         if (!$subnet->save()) {
             throw new Exception("Failed to create subnet.");
         }
         $network = IPTools\Network::parse($cidr);
         foreach ($network as $ip) {
             $range[] = ['subnet_id' => $subnet->id, 'ip' => ip2long((string) $ip)];
         }
         if (!SubnetIP::insert($range)) {
             throw new Exception("Failed to insert IP range.");
         }
     });
 }
Пример #2
0
$app->register(new Silex\Provider\RoutingServiceProvider());
$app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../app/views'));
$app->register(new Silex\Provider\HttpCacheServiceProvider(), array('http_cache.cache_dir' => __DIR__ . '/../app/cache/', 'http_cache.options' => ['default_ttl' => $app['cache_ttl']]));
$app->get('/', function () use($app) {
    return $app['twig']->render('index.html.twig');
})->bind('home');
$app->get('/swagger.json', function () use($app) {
    return new Symfony\Component\HttpFoundation\Response($app['twig']->render('swagger.json.twig'), 200, ['Content-Type' => 'application/json']);
})->bind('swagger');
$app->get('/apis.json', function () use($app) {
    return new Symfony\Component\HttpFoundation\Response($app['twig']->render('apis.json.twig'), 200, ['Content-Type' => 'application/json']);
})->bind('apisjson');
$app->get('/subnet/{ip}/{mask}', function ($ip, $mask) use($app) {
    $subnet = $ip . '/' . $mask;
    try {
        $subnet_info = IPTools\Network::parse($subnet)->info;
        unset($subnet_info['class']);
    } catch (Exception $e) {
        $app->abort(400, $e->getMessage());
    }
    return $app->json($subnet_info, 200, ['Cache-Control' => 's-maxage=' . $app['cache_ttl'] . ', public', 'ETag' => md5($subnet), 'Access-Control-Allow-Origin', '*']);
})->assert('ip', '[\\w\\.\\:]+')->assert('mask', '[0-9]+')->bind('api');
$app->after(function (Symfony\Component\HttpFoundation\Request $request, Symfony\Component\HttpFoundation\Response $response) {
    $response->headers->set('Access-Control-Allow-Origin', '*');
});
$app->error(function (\Exception $e, $code) use($app) {
    return $app->json(['error' => $e->getMessage()]);
});
if ($app['debug']) {
    $app->run();
} else {