LoginSignup
1
1

More than 5 years have passed since last update.

システム環境で設定されたプロキシーをwget, curlで使う

Last updated at Posted at 2012-07-27

Mac OSXではコマンドライン上からシステムで使用されているプロキシーの設定を見ることが難しい.そこで,システムのプロキシー設定を取得してexportを出力するプログラムを書いてみた.


#!/usr/bin/env php
<?php

function MacNetworkGetServiceName($interface = NULL) {
    if (!is_string($interface)) $interface = MacNetworkGetInterface();
    exec('networksetup -listnetworkserviceorder', $output);
    $network = FALSE;
    foreach ($output as $line) {
        if (preg_match('%^\(\d+\) (.+)$%m', $line, $match)) {
            $network = $match[1];
            continue;
        }
        if (preg_match('%Device: ([^, \)]+)%', $line, $match)) {
            $device = $match[1];
            if ($interface === $device) return $network;
        }
    }
    return NULL;
}

function MacNetworkGetInterface($ip_address = '0.0.0.0') {
    if (ip2long($ip_address) === FALSE) return NULL;
    exec("route -n get $ip_address", $output);
    foreach ($output as $line) {
        $line = array_map('trim', explode(':', trim($line), 2));
        if (count($line) != 2) continue;
        if (strtolower($line[0]) == 'interface') return $line[1];
    }
    return NULL;
}

function MacNetworkGetProxy($type = 'web', $service_name = NULL) {
    if (!is_string($service_name)) $service_name = MacNetworkGetServiceName();
    $type = strtolower($type);
    if (!in_array($type, array(
        'ftp', 'web', 'secureweb', 'streaming', 'gopher', 'socksfirewall'))) {
        return NULL;
    }
    exec("networksetup -get{$type}proxy $service_name", $output);
    $server = ''; $port = 0;
    foreach ($output as $line) {
        $line = array_map('trim', explode(':', trim($line), 2));
        if (count($line) != 2) continue;
        $key = strtolower($line[0]);
        if ($key == 'enabled') {
            if (in_array(strtolower($line[1]), array('no', 'false')) || !$line[1]) {
                return FALSE;
            }
        }
        if ($key == 'server') $server = $line[1];
        if ($key == 'port') $port = $line[1];
    }
    if ($server && $port) return "$server:$port";
    return FALSE;
}

$http_proxy = MacNetworkGetProxy('web');
$https_proxy = MacNetworkGetProxy('secureweb');
$socks_proxy = MacNetworkGetProxy('socksfirewall');


if ($http_proxy) {
    echo "export http_proxy='$http_proxy';";
    echo "export HTTP_PROXY='$http_proxy';";
    echo "export FTP_PROXY='http://$http_proxy/';";
    echo "export RSYNC_PROXY='$http_proxy';";
} else {
    echo "unset FTP_PROXY;unset RSYNC_PROXY;";
    echo "unset http_proxy;unset HTTP_PROXY;";
}
if ($https_proxy) {
    echo "export https_proxy='$https_proxy';";
    echo "export HTTPS_PROXY='$https_proxy';";
} else {
    echo "unset https_proxy;unset HTTPS_PROXY;";
}
if ($socks_proxy) {
    echo "export SOCKS_PROXY='$socks_proxy';";
} else {
    echo "unset SOCKS_PROXY;";
}
echo "\n";

上記プログラムを proxy_export という名前でパスを通し,

eval `proxy_export`

という感じで実行すると簡単に設定できる(かも).

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1