Java 类android.net.VpnService 实例源码

项目:Cybernet-VPN    文件:VpnAuthActivity.java   
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mConfig = getIntent().getStringExtra(KEY_CONFIG);
    mUsername = getIntent().getStringExtra(KEY_USERNAME);
    mPw = getIntent().getStringExtra(KEY_PASSWORD);
    Intent intent = VpnService.prepare(this);
    if (intent != null) {
        startActivityForResult(intent, 0);
    } else {
        startVpn();
        finish();
    }
}
项目:EasyVPN-Free    文件:ServerActivity.java   
private void startVpn() {
    stopwatch = new Stopwatch();
    connectedServer = currentServer;
    hideCurrentConnection = true;
    adbBlockCheck.setEnabled(false);

    Intent intent = VpnService.prepare(this);

    if (intent != null) {
        VpnStatus.updateStateString("USER_VPN_PERMISSION", "", R.string.state_user_vpn_permission,
                VpnStatus.ConnectionStatus.LEVEL_WAITING_FOR_USER_INPUT);
        // Start the query
        try {
            startActivityForResult(intent, START_VPN_PROFILE);
        } catch (ActivityNotFoundException ane) {
            // Shame on you Sony! At least one user reported that
            // an official Sony Xperia Arc S image triggers this exception
            VpnStatus.logError(R.string.no_vpn_support_image);
        }
    } else {
        onActivityResult(START_VPN_PROFILE, Activity.RESULT_OK, null);
    }
}
项目:colony    文件:OpenVPN.java   
private void startVPNWithProfile(VpnProfile vp) {
  Intent vpnPermissionIntent = VpnService.prepare(getBaseContext());
  int needPassword = vp.needUserPWInput(false);
  if (vpnPermissionIntent != null || needPassword != 0) {
    // VPN has not been prepared, this intent will prompt the user for
    // permissions, and subsequently launch OpenVPN.
    Intent shortVPNIntent = new Intent(Intent.ACTION_MAIN);
    shortVPNIntent.setClass(getBaseContext(), de.blinkt.openvpn.LaunchVPN.class);
    shortVPNIntent.putExtra(de.blinkt.openvpn.LaunchVPN.EXTRA_KEY, vp.getUUIDString());
    shortVPNIntent.putExtra(de.blinkt.openvpn.LaunchVPN.EXTRA_HIDELOG, true);
    shortVPNIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    this.cordova.getActivity().startActivity(shortVPNIntent);
  } else {
    Log.d(LOG_TAG, "Starting OpenVPN.");
    VPNLaunchHelper.startOpenVpn(vp, getBaseContext());
  }
}
项目:sigmavpn-android    文件:SigmaVPNClient.java   
public void startVPNService()
{
    if (!checkVPNServiceActive())
    {
        Intent intent = VpnService.prepare(this);

        if (intent != null)
            startActivityForResult(intent, 0);
        else
            onActivityResult(0, RESULT_OK, null);
    }
}
项目:Daedalus    文件:BootBroadcastReceiver.java   
@Override
public void onReceive(Context context, Intent intent) {
    if (Daedalus.getPrefs().getBoolean("settings_boot", false)) {
        Intent vIntent = VpnService.prepare(context);
        if (vIntent != null) {
            vIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(vIntent);
        }

        DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary());
        DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary());

        context.startService((new Intent(context, DaedalusVpnService.class)).setAction(DaedalusVpnService.ACTION_ACTIVATE));

        Logger.info("Triggered boot receiver");
    }

    Daedalus.updateShortcut(context);
}
项目:apc    文件:MainActivity.java   
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    int id = item.getItemId();

    if (id == R.id.action_settings)
    {
        startActivity(new Intent(this, SettingsActivity.class));
    }

    if (id == R.id.action_vpn)
    {
        Intent intent = VpnService.prepare(this);

        if (intent != null)
        {
            startActivityForResult(intent, VPN_REQUEST_CODE);
        }
        else
        {
            onActivityResult(VPN_REQUEST_CODE, RESULT_OK, null);
        }
    }

    return super.onOptionsItemSelected(item);
}
项目:dns66    文件:AdVpnService.java   
public static void checkStartVpnOnBoot(Context context) {
    Log.i("BOOT", "Checking whether to start ad buster on boot");
    Configuration config = FileHelper.loadCurrentSettings(context);
    if (config == null || !config.autoStart) {
        return;
    }
    if (!context.getSharedPreferences("state", MODE_PRIVATE).getBoolean("isActive", false)) {
        return;
    }

    if (VpnService.prepare(context) != null) {
        Log.i("BOOT", "VPN preparation not confirmed by user, changing enabled to false");
    }

    Log.i("BOOT", "Starting ad buster from boot");
    NotificationChannels.onCreate(context);

    Intent intent = getStartIntent(context);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && config.showNotification) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}
项目:dns66    文件:AdVpnThread.java   
private static Set<InetAddress> getDnsServers(Context context) throws VpnNetworkException {
    Set<InetAddress> out = new HashSet<>();
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(VpnService.CONNECTIVITY_SERVICE);
    // Seriously, Android? Seriously?
    NetworkInfo activeInfo = cm.getActiveNetworkInfo();
    if (activeInfo == null)
        throw new VpnNetworkException("No DNS Server");

    for (Network nw : cm.getAllNetworks()) {
        NetworkInfo ni = cm.getNetworkInfo(nw);
        if (ni == null || !ni.isConnected() || ni.getType() != activeInfo.getType()
                || ni.getSubtype() != activeInfo.getSubtype())
            continue;
        for (InetAddress address : cm.getLinkProperties(nw).getDnsServers())
            out.add(address);
    }
    return out;
}
项目:dns66    文件:AdVpnThread.java   
void newDNSServer(VpnService.Builder builder, String format, byte[] ipv6Template, InetAddress addr) throws UnknownHostException {
    // Optimally we'd allow either one, but the forwarder checks if upstream size is empty, so
    // we really need to acquire both an ipv6 and an ipv4 subnet.
    if (addr instanceof Inet6Address && ipv6Template == null) {
        Log.i(TAG, "newDNSServer: Ignoring DNS server " + addr);
    } else if (addr instanceof Inet4Address && format == null) {
        Log.i(TAG, "newDNSServer: Ignoring DNS server " + addr);
    } else if (addr instanceof Inet4Address) {
        upstreamDnsServers.add(addr);
        String alias = String.format(format, upstreamDnsServers.size() + 1);
        Log.i(TAG, "configure: Adding DNS Server " + addr + " as " + alias);
        builder.addDnsServer(alias);
        builder.addRoute(alias, 32);
        vpnWatchDog.setTarget(InetAddress.getByName(alias));
    } else if (addr instanceof Inet6Address) {
        upstreamDnsServers.add(addr);
        ipv6Template[ipv6Template.length - 1] = (byte) (upstreamDnsServers.size() + 1);
        InetAddress i6addr = Inet6Address.getByAddress(ipv6Template);
        Log.i(TAG, "configure: Adding DNS Server " + addr + " as " + i6addr);
        builder.addDnsServer(i6addr);
        vpnWatchDog.setTarget(i6addr);
    }
}
项目:apc    文件:MainActivity.java   
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    int id = item.getItemId();

    if (id == R.id.action_settings)
    {
        startActivity(new Intent(this, SettingsActivity.class));
    }

    if (id == R.id.action_vpn)
    {
        Intent intent = VpnService.prepare(this);

        if (intent != null)
        {
            startActivityForResult(intent, VPN_REQUEST_CODE);
        }
        else
        {
            onActivityResult(VPN_REQUEST_CODE, RESULT_OK, null);
        }
    }

    return super.onOptionsItemSelected(item);
}
项目:vpn    文件:ToyVpnClient.java   
@Override
public void onClick(View v) {
  Intent intent = VpnService.prepare(this);
  if (intent != null) {
    startActivityForResult(intent, 0);
  } else {
    onActivityResult(0, RESULT_OK, null);
  }
}
项目:ics-openconnect    文件:OpenConnectManagementThread.java   
private void addDefaultRoutes(VpnService.Builder b, LibOpenConnect.IPInfo ip, ArrayList<String> subnets) {
    boolean ip4def = true, ip6def = true;

    for (String s : subnets) {
        if (s.contains(":")) {
            ip6def = false;
        } else {
            ip4def = false;
        }
    }

    if (ip4def && ip.addr != null) {
        b.addRoute("0.0.0.0", 0);
        log("ROUTE: 0.0.0.0/0");
    }

    if (ip6def && ip.netmask6 != null) {
        b.addRoute("::", 0);
        log("ROUTE: ::/0");
    }
}
项目:star-dns-changer    文件:MainActivity.java   
private void startDNS() {
    if (presenter.isWorking()) {
        presenter.stopService();
    } else if (isValid()) {
        Intent intent = VpnService.prepare(this);
        if (intent != null) {
            startActivityForResult(intent, REQUEST_CONNECT);
        } else {
            onActivityResult(REQUEST_CONNECT, RESULT_OK, null);
        }
    } else {
        makeSnackbar(getString(R.string.enter_valid_dns));
    }
}
项目:gnirehtet    文件:GnirehtetControlReceiver.java   
private void startGnirehtet(Context context, VpnConfiguration config) {
    Intent vpnIntent = VpnService.prepare(context);
    if (vpnIntent == null) {
        Log.d(TAG, "VPN was already authorized");
        // we got the permission, start the service now
        GnirehtetService.start(context, config);
    } else {
        Log.w(TAG, "VPN requires the authorization from the user, requesting...");
        requestAuthorization(context, vpnIntent, config);
    }
}
项目:SSLSocks    文件:StunnelVpnService.java   
@Override
public void onCreate() {
    super.onCreate();
    builder = new VpnService.Builder();
    builder.addAddress(VPN_ADDRESS, 32);
    builder.addRoute(VPN_ROUTE, 0);
    builder.establish();
}
项目:Virtual-Hosts    文件:MainActivity.java   
private void startVPN() {
    waitingForVPNStart = false;
    Intent vpnIntent = VpnService.prepare(this);
    if (vpnIntent != null)
        startActivityForResult(vpnIntent, VPN_REQUEST_CODE);
    else
        onActivityResult(VPN_REQUEST_CODE, RESULT_OK, null);
}
项目:Android-Firewall    文件:Receiver.java   
@Override
public void onReceive(final Context context, Intent intent) {
    Log.i(TAG, "Received " + intent);
    Util.logExtras(TAG, intent);

    // Start service
    if (VpnService.prepare(context) == null)
        BlackHoleService.start(context);
}
项目:Daedalus    文件:Daedalus.java   
public boolean activateService() {
    Intent intent = VpnService.prepare(Daedalus.getInstance());
    if (intent != null) {
        return false;
    } else {
        DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary());
        DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary());
        startService(Daedalus.getInstance().getServiceIntent().setAction(DaedalusVpnService.ACTION_ACTIVATE));
        return true;
    }
}
项目:NetKnight    文件:NetOutput.java   
public NetOutput(BlockingQueue<Packet> inputQueue, BlockingQueue<ByteBuffer> outputQueue, VpnService vpnService, Selector selector,BlockingQueue<App> appCacheQueue) {
    mOutputQueue = outputQueue;
    mInputQueue = inputQueue;

    mVpnService = vpnService;
    mChannelSelector = selector;
    mAppCacheQueue = appCacheQueue;
}
项目:minivtun-android    文件:MainActivity.java   
@Override
public void onClick(View v) {
    Intent intent = VpnService.prepare(this);
    if (intent != null) {
        startActivityForResult(intent, 0);
    } else {
        onActivityResult(0, RESULT_OK, null);
    }
}
项目:minivtun-android    文件:MinivtunService.java   
private void configure(String parameters) throws Exception {
    // If the old interface has exactly the same parameters, use it!
    if (mInterface != null) {
        Log.i(TAG, "Using the previous interface");
        return;
    }

    // Configure a builder while parsing the parameters.
    VpnService.Builder builder = new VpnService.Builder();

    builder.addAddress(mTunAddress, mTunPrefixLen);
    Log.i(TAG, "set tunnel address: " + mTunAddress + "/" + mTunPrefixLen);
    builder.setMtu(mMTU);
    // add US routes
    builder.addRoute("0.0.0.0", 0);
    builder.addDnsServer("8.8.8.8");

    // Close the old interface since the parameters have been changed.
    try {
        mInterface.close();
    } catch (Exception e) {
        // ignore
    }

    // Create a new interface using the builder and save the parameters.
    mInterface = builder.setSession(mServerAddress)
            .setConfigureIntent(mConfigureIntent)
            .establish();
    //mParameters = parameters;
    Log.i(TAG, "New interface: " + parameters);
}
项目:dns66    文件:StartFragment.java   
private void startService() {
    Log.i(TAG, "Attempting to connect");
    Intent intent = VpnService.prepare(getContext());
    if (intent != null) {
        startActivityForResult(intent, REQUEST_START_VPN);
    } else {
        onActivityResult(REQUEST_START_VPN, RESULT_OK, null);
    }
}
项目:capture    文件:MainActivity.java   
private void requestVPN() {
    Intent vpnIntent = VpnService.prepare(this);
    if (vpnIntent != null) {
        startActivityForResult(vpnIntent, VPN_REQUEST_CODE);
    } else {
        startVPN();
    }
}
项目:VpnProxy    文件:LocalVpnService.java   
@Override
public IBinder onBind(Intent intent) {
    String action = intent.getAction();
    if (action.equals(VpnService.SERVICE_INTERFACE)) {
        return super.onBind(intent);
    }
    return null;
}
项目:block-this    文件:VpnFragment.java   
public void startVpn(){
    Intent intent = VpnService.prepare(getActivity());
    if (intent != null) {
        startActivityForResult(intent, 0);
    } else {
        onActivityResult(0, Activity.RESULT_OK, null);
    }
}
项目:block-this    文件:AutoLaunchActivity.java   
protected void connect(){
        Intent intent = VpnService.prepare(this);
        if (intent != null) {
            startActivityForResult(intent, 0);
        } else {
            onActivityResult(0, RESULT_OK, null);
        }
}
项目:xTun-android    文件:xTunVpnService.java   
@Override
public IBinder onBind(Intent intent) {
    String action = intent.getAction();
    if (VpnService.SERVICE_INTERFACE.equals(action)) {
        return super.onBind(intent);
    } else if (Constants.Action.SERVICE.equals(action)) {
        return binder;
    }
    return null;
}
项目:xTun-android    文件:MainActivity.java   
private void prepareStartService() {
    showProgress(getString(R.string.connecting));
    int REQUEST_CONNECT = 1;
    Intent intent = VpnService.prepare(this);
    if (intent != null) {
        startActivityForResult(intent, REQUEST_CONNECT);
    } else {
        onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null);
    }
}
项目:xTun-android    文件:xTunRunnerActivity.java   
private void startBackgroundService() {
    Intent intent = VpnService.prepare(xTunRunnerActivity.this);
    int REQUEST_CONNECT = 1;
    if (intent != null) {
        startActivityForResult(intent, REQUEST_CONNECT);
    } else {
        onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null);
    }
}
项目:BaoLianDeng    文件:LocalVpnService.java   
@Override
public IBinder onBind(Intent intent) {
    String action = intent.getAction();
    if (action.equals(VpnService.SERVICE_INTERFACE)) {
        return super.onBind(intent);
    }
    return null;
}
项目:android-openvpn-settings    文件:ApiLevel14.java   
@Override
public boolean prepareVpnService(Activity activity, int requestCode)
{
    Intent prepare = VpnService.prepare( activity.getApplicationContext() );
    if ( prepare != null )
    {
        activity.startActivityForResult( prepare, requestCode );
        return false; // VpnService is not prepared and the user will receive a dialog.
    }
    else
    {
        return true; // VpnService has already been prepared, user not asked again.
    }
}
项目:DNSman    文件:VpnWrapperActivity.java   
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent dnsData = getIntent();
    dns1 = dnsData.getStringExtra(ValueConstants.EXTRA_DNS1);
    dns2 = dnsData.getStringExtra(ValueConstants.EXTRA_DNS2);

    Intent i = VpnService.prepare(this);
    if (i != null) {
        startActivityForResult(i, ValueConstants.REQUEST_VPN);
    } else {
        launchServiceWithTimeDelay(this, dns1, dns2);
    }
}
项目:product-emm    文件:AlertActivity.java   
/**
 * This method starts a VPN connection.
 */
private void startVpn() {

    try {
        JSONObject vpnData = new JSONObject(payload);
        if (!vpnData.isNull(resources.getString(R.string.intent_extra_server))) {
            serverAddress = (String) vpnData.get(resources.getString(R.string.intent_extra_server));
        }
        if (!vpnData.isNull(resources.getString(R.string.intent_extra_server_port))) {
            serverPort = (String) vpnData.get(resources.getString(R.string.intent_extra_server_port));
        }

        if (!vpnData.isNull(resources.getString(R.string.intent_extra_shared_secret))) {
            sharedSecret = (String) vpnData.get(resources.getString(R.string.intent_extra_shared_secret));
        }

        if (!vpnData.isNull(resources.getString(R.string.intent_extra_dns))) {
            dnsServer = (String) vpnData.get(resources.getString(R.string.intent_extra_dns));
        }
    } catch (JSONException e) {
        Log.e(TAG, "Invalid VPN payload " + e);
    }

    Intent intent = VpnService.prepare(this);
    if (intent != null) {
        startActivityForResult(intent, VPN_REQUEST_CODE);
    } else {
        onActivityResult(VPN_REQUEST_CODE, RESULT_OK, null);
    }
}
项目:LocalVPN    文件:LocalVPN.java   
private void startVPN()
{
    Intent vpnIntent = VpnService.prepare(this);
    if (vpnIntent != null)
        startActivityForResult(vpnIntent, VPN_REQUEST_CODE);
    else
        onActivityResult(VPN_REQUEST_CODE, RESULT_OK, null);
}
项目:Safejumper-for-Android    文件:AppActivity.java   
private void launchVPN () {

            if (mActiveVpnProfile == null)
                return;
            moveOptionsToConnection(mActiveVpnProfile);
            int vpnok = mActiveVpnProfile.checkProfile(AppActivity.this);
            if(vpnok!= R.string.no_error_found) {
                Log.e(TAG, "checkProfile failed: "+getString(vpnok));
                return;
            }

            Intent intent = VpnService.prepare(AppActivity.this);

            // Check if we want to fix /dev/tun
            boolean usecm9fix = false;
            boolean loadTunModule = false;

            if(loadTunModule)
                execeuteSUcmd("insmod /system/lib/modules/tun.ko");

            if(usecm9fix && !mCmfixed ) {
                execeuteSUcmd("chown system /dev/tun");
            }

            if (intent != null) {
                VpnStatus.updateStateString("USER_VPN_PERMISSION", "", R.string.state_user_vpn_permission,
                        ConnectionStatus.LEVEL_WAITING_FOR_USER_INPUT);
                //start query
                try {
                    startActivityForResult(intent, START_VPN_CMD);
                } catch (ActivityNotFoundException ane) {
                    VpnStatus.logError(R.string.no_vpn_support_image);
                }
            } else {
                onActivityResult(START_VPN_CMD, Activity.RESULT_OK, null);
            }
        }
项目:ics-openconnect    文件:ExternalOpenVPNService.java   
@Override
public Intent prepareVPNService() throws RemoteException {
    checkOpenVPNPermission();

    if(VpnService.prepare(ExternalOpenVPNService.this)==null)
        return null;
    else 
        return new  Intent(getBaseContext(), GrantPermissionsActivity.class);
}
项目:ics-openconnect    文件:OpenConnectManagementThread.java   
private void addSubnetRoutes(VpnService.Builder b, LibOpenConnect.IPInfo ip, ArrayList<String> subnets) {
    for (String s : subnets) {
        s = s.trim();
        try {
            if (s.contains(":")) {
                String ss[] = s.split("/");
                if (ss.length == 1) {
                    b.addRoute(ss[0], 128);
                } else {
                    b.addRoute(ss[0], Integer.parseInt(ss[1]));
                }
                log("ROUTE: " + s);
            } else {
                CIDRIP cdr;
                if (!s.contains("/")) {
                    cdr = new CIDRIP(s + "/32");
                } else {
                    cdr = new CIDRIP(s);
                }
                b.addRoute(cdr.mIp, cdr.len);
                log("ROUTE: " + cdr.mIp + "/" + cdr.len);
            }
        } catch (Exception e) {
            log("ROUTE: skipping invalid route '" + s + "'");
        }
    }
}
项目:MKAPP    文件:ServiceSinkhole.java   
@Override
public VpnService.Builder setMtu(int mtu) {
    this.mtu = mtu;
    super.setMtu(mtu);
    return this;
}
项目:gnirehtet    文件:RelayTunnel.java   
public static RelayTunnel open(VpnService vpnService) throws IOException {
    Log.d(TAG, "Opening a new relay tunnel...");
    SocketChannel channel = SocketChannel.open();
    vpnService.protect(channel.socket());
    return new RelayTunnel(channel);
}
项目:gnirehtet    文件:Forwarder.java   
public Forwarder(VpnService vpnService, FileDescriptor vpnFileDescriptor, RelayTunnelListener listener) {
    this.vpnFileDescriptor = vpnFileDescriptor;
    tunnel = new PersistentRelayTunnel(vpnService, listener);
}