Advertisement
sulitnetsolutions

Untitled

Jan 11th, 2020
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.46 KB | None | 0 0
  1. package renz.javacodez.openvpn;
  2.  
  3. import android.support.v7.app.AlertDialog;
  4. import android.os.*;
  5. import android.widget.*;
  6. import android.view.*;
  7. import android.view.View.*;
  8. import android.widget.AdapterView.*;
  9. import de.blinkt.openvpn.core.*;
  10. import renz.javacodez.openvpn.adapter.AdapterHelper.*;
  11. import java.util.*;
  12. import java.io.*;
  13. import android.preference.*;
  14. import android.content.*;
  15. import tsholo.corp.tsholovpn.R;
  16. import android.support.design.widget.*;
  17. import static de.blinkt.openvpn.core.ConnectionStatus.*;
  18. import static renz.javacodez.openvpn.RenzGenerator.*;
  19. import renz.javacodez.openvpn.helper.NavigationHelper.*;
  20. import renz.javacodez.openvpn.json.JsonManager.*;
  21. import renz.javacodez.openvpn.helper.*;
  22. import android.content.SharedPreferences.*;
  23. import de.blinkt.openvpn.*;
  24. import org.json.*;
  25. import de.blinkt.openvpn.activities.*;
  26. import android.support.v7.app.*;
  27. import android.net.*;
  28. import android.support.v7.widget.Toolbar;
  29. import android.support.v7.app.ActionBarDrawerToggle;
  30. import android.support.v4.widget.*;
  31. import android.support.v4.view.*;
  32. import android.support.v4.app.ActivityCompat;
  33. import com.github.angads25.filepicker.model.*;
  34. import com.github.angads25.filepicker.view.*;
  35. import com.github.angads25.filepicker.controller.*;
  36. import android.support.v4.content.*;
  37. import de.blinkt.openvpn.fragments.*;
  38. import renz.javacodez.openvpn.util.*;
  39. import renz.javacodez.openvpn.sshcore.*;
  40. import renz.javacodez.openvpn.openvpncore.*;
  41. import renz.javacodez.openvpn.logger.*;
  42. import renz.javacodez.openvpn.view.LogView;
  43. import renz.javacodez.openvpn.view.*;
  44. import renz.javacodez.openvpn.view.DataTransferGraph;
  45. import android.graphics.*;
  46. import com.sdsmdg.tastytoast.*;
  47. import android.content.pm.*;
  48. import android.*;
  49. import java.text.*;
  50. import com.github.mikephil.charting.data.*;
  51. import com.github.mikephil.charting.charts.*;
  52.  
  53. public class Main extends MainBase implements ActivityCompat.OnRequestPermissionsResultCallback, RadioGroup.OnCheckedChangeListener,
  54. SSHTunnelCore.VPNListener, ServerUpdate.OnUpdateListener,
  55. OnClickListener, OnItemSelectedListener,
  56. VpnStatus.StateListener, VpnStatus.ByteCountListener,
  57. GeneratorHelper.CancelListener, BottomNavigationView.OnNavigationItemSelectedListener,
  58. ServerDuration.DurationListener
  59. {
  60. private String USERNAME = "VPN_USERNAME";
  61. private String PASSWORD = "VPN_PASSWORD";
  62. private String SELECTED_SERVER = "SELECTED_SERVER";
  63. private String SELECTED_NETWORK = "SELECTED_NETWORK";
  64. private String USE_PAYLOAD = "USE_PAYLOAD";
  65. private Spinner server_spin;
  66. private Spinner network_spin;
  67. private Spinner category_spin;
  68. private RadioGroup connect_type;
  69. private TextView status;
  70. private EditText username;
  71. private MaterialEditText password;
  72. private ToggleButton use_payload;
  73. private SharedPreferences prefs;
  74. private SharedPreferences.Editor editor;
  75. private List<JSONObject> listServer;
  76. private ServerAdapter serverAdapter;
  77. private List<String> listNetwork;
  78. private NetworkAdapter networkAdapter;
  79. private View graphView, bannerView;
  80. private Button connect,disconnect;
  81. private BottomNavigationView bottomNavi;
  82. private TextView configVersion;
  83. private GeneratorHelper gh;
  84. private EditText custom_payload_input;
  85. private TextView prem, vip, priv;
  86. private ProgressBar pb;
  87. private LineChart mChart;
  88. private Thread dataUpdate;
  89. private Handler vHandler = new Handler();
  90. DecimalFormat df = new DecimalFormat("#.##");
  91. private GraphHelper graph;
  92. private ImageButton bt_toggle_input;
  93. private Button bt_hide_input;
  94. private Button bt_save_input;
  95. private View lyt_expand_input;
  96. private NestedScrollView nested_scroll_view;
  97. private TextView upBytes,dlBytes,elapsedTime;
  98. private Timer timer;
  99.  
  100. @Override
  101. protected void onCreate(Bundle savedInstanceState)
  102. {
  103. // TODO: Implement this method
  104. super.onCreate(savedInstanceState);
  105. setContentView(R.layout.main);
  106.  
  107. prefs = PreferenceManager.getDefaultSharedPreferences(Main.this);
  108. editor = prefs.edit();
  109. SSHTunnelCore.setVPNListener(this);
  110. VpnStatus.addStatusListener(this);
  111.  
  112. category_spin = (Spinner)findViewById(R.id.category_spin);
  113. server_spin = (Spinner)findViewById(R.id.server_list);
  114. network_spin = (Spinner)findViewById(R.id.network_list);
  115. status = (TextView)findViewById(R.id.status);
  116. username = (EditText)findViewById(R.id.vpn_username);
  117. password = (MaterialEditText)findViewById(R.id.vpn_password);
  118. connect = (Button)findViewById(R.id.connect_vpn);
  119. disconnect = (Button)findViewById(R.id.disconnect_vpn);
  120. use_payload = (ToggleButton)findViewById(R.id.custom_payload);
  121. connect_type = (RadioGroup)findViewById(R.id.connection_type);
  122. bottomNavi = (BottomNavigationView)findViewById(R.id.bottom_navi);
  123. graphView = findViewById(R.id.graphView);
  124. bannerView = findViewById(R.id.bannerView);
  125. configVersion = (TextView)findViewById(R.id.config_version);
  126. upBytes = (TextView)findViewById(R.id.upload);
  127. dlBytes = (TextView)findViewById(R.id.download);
  128. elapsedTime = (TextView)findViewById(R.id.duration);
  129.  
  130. initComponent();
  131.  
  132. String[] categories = new String[]{"PREMIUM"};
  133. ArrayAdapter<String> categoryAdapter = new ArrayAdapter<String>(this, R.layout.category_item, categories);
  134. category_spin.setAdapter(categoryAdapter);
  135.  
  136. listServer = new ArrayList<>();
  137. serverAdapter = new ServerAdapter(this, listServer);
  138. server_spin.setAdapter(serverAdapter);
  139. //setupServer();
  140.  
  141. listNetwork = new ArrayList<String>();
  142. networkAdapter = new NetworkAdapter(this, listNetwork);
  143. network_spin.setAdapter(networkAdapter);
  144.  
  145. configVersion.setText(Util.getConfigVersion(this));
  146. if (ConfigUtil.getInstance(this).getConnectionType() == 2) {
  147. loadSSLNetworks();
  148. } else {
  149. loadDefaultNetwork();
  150. }
  151.  
  152. Util.disableShiftMode(bottomNavi);
  153. connect.setOnClickListener(this);
  154. disconnect.setOnClickListener(this);
  155. connect_type.setOnCheckedChangeListener(this);
  156. use_payload.setOnClickListener(this);
  157. category_spin.setOnItemSelectedListener(this);
  158. server_spin.setOnItemSelectedListener(this);
  159. network_spin.setOnItemSelectedListener(this);
  160. bottomNavi.setOnNavigationItemSelectedListener(this);
  161.  
  162. username.setText(prefs.getString(USERNAME, ""));
  163. password.setText(prefs.getString(PASSWORD, ""));
  164. category_spin.setSelection(prefs.getInt("Category", 0));
  165. network_spin.setSelection(prefs.getInt(SELECTED_NETWORK, 0));
  166. ((RadioButton)connect_type.getChildAt(ConfigUtil.getInstance(this).getConnectionType())).setChecked(true);
  167.  
  168. mChart = (LineChart) findViewById(R.id.chart1);
  169. graph = GraphHelper.getHelper().with(this).color(getColor(R.color.graph_color)).chart(mChart);
  170.  
  171. liveData();
  172.  
  173. renderLastEvent();
  174. }
  175.  
  176. private void initComponent() {
  177.  
  178. // input section
  179. nested_scroll_view = (NestedScrollView) findViewById(R.id.nested_scroll_view);
  180. bt_toggle_input = (ImageButton) findViewById(R.id.bt_toggle_input);
  181. bt_hide_input = (Button) findViewById(R.id.bt_hide_input);
  182. bt_save_input = (Button) findViewById(R.id.bt_save_input);
  183. lyt_expand_input = (View) findViewById(R.id.lyt_expand_input);
  184. lyt_expand_input.setVisibility(View.GONE);
  185.  
  186. bt_toggle_input.setOnClickListener(new View.OnClickListener() {
  187. @Override
  188. public void onClick(View view) {
  189. toggleSectionInput(bt_toggle_input);
  190. }
  191. });
  192.  
  193. bt_hide_input.setOnClickListener(new View.OnClickListener() {
  194. @Override
  195. public void onClick(View view) {
  196. toggleSectionInput(bt_toggle_input);
  197. }
  198. });
  199.  
  200. bt_save_input.setOnClickListener(new View.OnClickListener() {
  201. @Override
  202. public void onClick(View view) {
  203. String user = username.getText().toString();
  204. String pass = password.getText().toString();
  205. editor.putString(USERNAME, user).apply();
  206. editor.putString(PASSWORD, pass).apply();
  207. toggleSectionInput(bt_toggle_input);
  208. }
  209. });
  210. }
  211.  
  212.  
  213.  
  214. private void toggleSectionInput(View view) {
  215. boolean show = toggleArrow(view);
  216. if (show) {
  217. ViewAnimation.expand(lyt_expand_input, new ViewAnimation.AnimListener() {
  218. @Override
  219. public void onFinish() {
  220. ViewAnimation.nestedScrollTo(nested_scroll_view, lyt_expand_input);
  221. }
  222. });
  223. } else {
  224. ViewAnimation.collapse(lyt_expand_input);
  225. }
  226. }
  227.  
  228. public boolean toggleArrow(View view) {
  229. if (view.getRotation() == 0) {
  230. view.animate().setDuration(200).rotation(180);
  231. return true;
  232. } else {
  233. view.animate().setDuration(200).rotation(0);
  234. return false;
  235. }
  236. }
  237. public void liveData()
  238. {
  239.  
  240. dataUpdate = new Thread(new Runnable() {
  241. @Override
  242. public void run()
  243. {
  244.  
  245. while (!dataUpdate.getName().equals("stopped"))
  246. {
  247.  
  248. vHandler.post(new Runnable() {
  249.  
  250. @Override
  251. public void run()
  252. {
  253. //addDataSet();
  254. if (InjectorService.isRunning)
  255. {
  256. graph.start();
  257. }
  258. else
  259. {
  260. graph.stop();
  261. }
  262. }
  263. });
  264.  
  265. try
  266. {
  267. Thread.sleep(1000);
  268. }
  269. catch (InterruptedException e)
  270. {
  271. e.printStackTrace();
  272. }
  273. // progressStatus--;
  274. }
  275.  
  276. }
  277. });
  278.  
  279. dataUpdate.setName("started");
  280. dataUpdate.start();
  281.  
  282. }
  283.  
  284. @Override
  285. public void onCheckedChanged(RadioGroup p1, int p2)
  286. {
  287. switch (p1.getId()) {
  288. case R.id.connection_type:
  289. RadioButton v = (RadioButton)p1.findViewById(p2);
  290. int index = p1.indexOfChild(v);
  291. if (index == 2) {
  292. if (listNetwork.size() > 0) {
  293. listNetwork.clear();
  294. networkAdapter.notifyDataSetChanged();
  295. }
  296. loadSSLNetworks();
  297. } else {
  298. if (listNetwork.size() > 0) {
  299. listNetwork.clear();
  300. networkAdapter.notifyDataSetChanged();
  301. }
  302. loadDefaultNetwork();
  303. }
  304. ConfigUtil.getInstance(this).setConnectionType(index);
  305. break;
  306. }
  307. // TODO: Implement this method
  308. }
  309.  
  310. @Override
  311. public boolean onNavigationItemSelected(MenuItem p1)
  312. {
  313. switch (p1.getItemId()) {
  314. case R.id.menu_update:
  315. checkUpdates();
  316. break;
  317. case R.id.menu_showLog:
  318. showLog();
  319. break;
  320. //case R.id.menu_contact_us:
  321. //viewWeb(getString(R.string.link_facebook));
  322. //break;
  323. case R.id.menu_about:
  324. about();
  325. break;
  326. case R.id.menu_exit:
  327. finish();
  328. break;
  329. }
  330. // TODO: Implement this method
  331. return true;
  332. }
  333.  
  334. private void showDurationDialog()
  335. {
  336. View v = getLayoutInflater().inflate(R.layout.duration_dialog, null);
  337. prem = (TextView)v.findViewById(R.id.premium_duration);
  338. vip = (TextView)v.findViewById(R.id.vip_duration);
  339. priv = (TextView)v.findViewById(R.id.private_duration);
  340. pb = (ProgressBar)v.findViewById(R.id.duration_progress);
  341. Button refresh = (Button)v.findViewById(R.id.refresh_duration);
  342. Button hide = (Button)v.findViewById(R.id.hide_duration);
  343.  
  344. prem.setText("--/--");
  345. vip.setText("--/--");
  346. priv.setText("--/--");
  347. pb.setVisibility(View.VISIBLE);
  348.  
  349. final ServerDuration sd = new ServerDuration();
  350. sd.setListener(this);
  351. sd.setUserPass(username.getText().toString(), password.getText().toString());
  352. try {
  353. sd.start();
  354. } catch (Exception e) {
  355. }
  356. final AlertDialog dialog = new AlertDialog.Builder(this).create();
  357. dialog.setView(v);
  358.  
  359. refresh.setOnClickListener(new OnClickListener() {
  360.  
  361. @Override
  362. public void onClick(View p1)
  363. {
  364. try {
  365. sd.start();
  366. } catch (Exception e) {
  367.  
  368. }
  369. pb.setVisibility(View.VISIBLE);
  370. showToast("Refreshing. Please wait...");
  371. // TODO: Implement this method
  372. }
  373.  
  374.  
  375. });
  376. hide.setOnClickListener(new OnClickListener() {
  377.  
  378. @Override
  379. public void onClick(View p1)
  380. {
  381. dialog.dismiss();
  382. // TODO: Implement this method
  383. }
  384.  
  385.  
  386. });
  387. dialog.show();
  388. // TODO: Implement this method
  389. }
  390.  
  391. @Override
  392. public void onShowDuration(ServerDuration.Duration duration)
  393. {
  394. pb.setVisibility(View.GONE);
  395. prem.setText(duration.premium);
  396. vip.setText(duration.vip);
  397. priv.setText(duration.priv);
  398. // TODO: Implement this method
  399. }
  400.  
  401. @Override
  402. public void onError(String error)
  403. {
  404. pb.setVisibility(View.GONE);
  405. prem.setText("--/--");
  406. vip.setText("--/--");
  407. priv.setText("--/--");
  408. showToast(error);
  409. // TODO: Implement this method
  410. }
  411. private void showLog()
  412. {
  413. int type = connect_type.getCheckedRadioButtonId();
  414. Intent intent = new Intent();
  415. if (type == R.id.type_openvpn) {
  416. intent.setClass(this, LogWindow.class);
  417. } else {
  418. intent.setClass(this, LogActivity.class);
  419. }
  420. startActivity(intent);
  421. // TODO: Implement this method
  422. }
  423. public void checkUpdates()
  424. {
  425. final ServerUpdate su = new ServerUpdate(this);
  426. su.setURL(getString(R.string.link_update));
  427. su.setUpdateListener(this);
  428.  
  429. CharSequence[] titles = {"Online","Offline"};
  430. AlertDialog.Builder ab = new AlertDialog.Builder(this);
  431. ab.setIcon(R.drawable.ic_update_dialog);
  432. ab.setTitle("Updates");
  433. ab.setSingleChoiceItems(titles, 0, new DialogInterface.OnClickListener()
  434. {
  435.  
  436. @Override
  437. public void onClick(DialogInterface p1, int p2)
  438. {
  439. switch (p2) {
  440. case 0:
  441. su.start();
  442. break;
  443. case 1:
  444. startOfflineUpdate();
  445. break;
  446. }
  447. p1.dismiss();
  448. // TODO: Implement this method
  449. }
  450.  
  451.  
  452. }).setNegativeButton("Cancel", null);
  453. ab.show();
  454. }
  455. private void startOfflineUpdate()
  456. {
  457. String perm = Manifest.permission.READ_EXTERNAL_STORAGE;
  458. if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
  459. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
  460. } else {
  461. showFilePicker();
  462. }
  463. }
  464.  
  465. private void showFilePicker()
  466. {
  467. DialogProperties dp = new DialogProperties();
  468.  
  469. dp.selection_mode = DialogConfigs.SINGLE_MODE;
  470. dp.selection_type = DialogConfigs.FILE_SELECT;
  471. dp.root = Environment.getExternalStorageDirectory();
  472.  
  473. FilePickerDialog picker = new FilePickerDialog(this, dp);
  474. picker.setTitle("Select JsonFile");
  475. picker.setDialogSelectionListener(new DialogSelectionListener()
  476. {
  477.  
  478. @Override
  479. public void onSelectedFilePaths(String[] files)
  480. {
  481. for (String file: files) {
  482. if (file.endsWith(".js")) {
  483. startImport(Main.this, file);
  484. } else {
  485. showToast("The file extension must end with .js");
  486. }
  487. }
  488. // TODO: Implement this method
  489. }
  490.  
  491.  
  492. });
  493. picker.setPositiveBtnName("Select");
  494. picker.setNegativeBtnName("Cancel");
  495. picker.show();
  496. // TODO: Implement this method
  497. }
  498.  
  499. @Override
  500. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
  501. {
  502. switch (requestCode) {
  503. case 1:
  504. if (grantResults.length > 0) {
  505. for (int grantResult: grantResults) {
  506. if (grantResult == PackageManager.PERMISSION_GRANTED) {
  507. showFilePicker();
  508. } else {
  509. showToast("You need to grant the permission to use offline update!");
  510. }
  511. }
  512. }
  513. break;
  514. }
  515. // TODO: Implement this method
  516. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  517. }
  518.  
  519. @Override
  520. public void onShowUpdate(String newVersion)
  521. {
  522. AlertDialog.Builder ab = new AlertDialog.Builder(this);
  523. ab.setTitle("Updates Found");
  524. ab.setMessage("New version found: "+newVersion +", do you want to update?");
  525. ab.setPositiveButton("Update",new DialogInterface.OnClickListener()
  526. {
  527. @Override
  528. public void onClick(DialogInterface p0, int p1)
  529. {
  530. if (getPM().getProfiles().size() > 0) {
  531. try {
  532. for (VpnProfile vp: getPM().getProfiles()) {
  533. getPM().removeProfile(Main.this, vp);
  534. }
  535. } catch (Exception e) {
  536.  
  537. }
  538. }
  539.  
  540. loadServers(0);
  541. loadDefaultNetwork();
  542. showToast("Update Success!");
  543. }
  544. });
  545. ab.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
  546. {
  547. @Override
  548. public void onClick(DialogInterface p0, int p1)
  549. {
  550. File file = new File(getFilesDir(), "Servers.js");
  551. file.delete();
  552. }
  553. });
  554. ab.show();
  555. }
  556. @Override
  557. public void onNoUpdateAvailable(String oldVersion)
  558. {
  559. AlertDialog.Builder ab = new AlertDialog.Builder(this);
  560. ab.setTitle("No Updates");
  561. ab.setMessage("No updates available, your current server version is: " + oldVersion);
  562. ab.setPositiveButton("Ok", null);
  563. ab.show();
  564. }
  565.  
  566.  
  567. public void loadDefaultNetwork()
  568. {
  569. try {
  570. if (listNetwork.size() > 0) {
  571. listNetwork.clear();
  572. networkAdapter.notifyDataSetChanged();
  573. }
  574.  
  575. JSONArray network = getNetworksArray();
  576. for (int i = 0; i < network.length(); i++) {
  577. listNetwork.add(network.getJSONObject(i).getString("Name"));
  578. networkAdapter.notifyDataSetChanged();
  579. }
  580. } catch (Exception e) {
  581. showToast(e.getMessage());
  582. }
  583. }
  584. private void loadSSLNetworks()
  585. {
  586. try {
  587. JSONArray sslNetwork = getSSLNetworksArray();
  588. for (int i = 0; i < sslNetwork.length(); i++) {
  589. listNetwork.add(sslNetwork.getJSONObject(i).getString("Name"));
  590. networkAdapter.notifyDataSetChanged();
  591. }
  592. } catch (Exception e) {
  593. showToast(e.getMessage());
  594. }
  595. }
  596. @Override
  597. public void onClick(View p1)
  598. {
  599. int id = p1.getId();
  600. switch (id) {
  601. case R.id.connect_vpn:
  602. prepareToStart();
  603. break;
  604. case R.id.disconnect_vpn:
  605. stopVPN();
  606. break;
  607. case R.id.custom_payload:
  608. if (use_payload.isChecked()) {
  609. showCustomPayload();
  610. }
  611. break;
  612. }
  613. // TODO: Implement this method
  614. }
  615.  
  616. @Override
  617. public void onItemSelected(AdapterView<?> p1, View p2, int p3, long p4)
  618. {
  619. switch (p1.getId()) {
  620. case R.id.category_spin:
  621. loadServers(p3);
  622. String str = (String)p1.getSelectedItem();
  623. server_spin.setSelection(prefs.getInt(SELECTED_SERVER + str, 0));
  624. editor.putInt("Category", p3).apply();
  625. break;
  626. case R.id.server_list:
  627. String str2 = (String)category_spin.getSelectedItem();
  628. editor.putInt(SELECTED_SERVER + str2, p3).apply();
  629.  
  630. try {
  631. String serverName = ((JSONObject)p1.getSelectedItem()).getString("Name");
  632. use_payload.setChecked(prefs.getBoolean(USE_PAYLOAD + serverName, false));
  633. } catch (Exception e) {
  634.  
  635. }
  636. break;
  637. case R.id.network_list:
  638. editor.putInt(SELECTED_NETWORK, p3).apply();
  639. break;
  640. }
  641. // TODO: Implement this method
  642. }
  643. public void loadServers(int category)
  644. {
  645. try {
  646. if (listServer.size() > 0) {
  647. listServer.clear();
  648. serverAdapter.notifyDataSetChanged();
  649. }
  650. ArrayList<JSONObject> listObject = new ArrayList<>();
  651. JSONArray serverJs = getServersArray();
  652. JSONObject obj = null;
  653.  
  654. if (listObject.size() > 0) {
  655. listObject.clear();
  656. }
  657. for (int i = 0; i < serverJs.length(); i++) {
  658. obj = serverJs.getJSONObject(i);
  659. String cat = obj.getString("Category");
  660. if (obj.has("Category")) {
  661. if (category == 0) {
  662. if (cat.equals("PREMIUM")) {
  663. listObject.add(obj);
  664. }
  665. } else if (category == 1) {
  666. if (cat.equals("VIP")) {
  667. listObject.add(obj);
  668. }
  669. } else if (category == 2) {
  670. if (cat.equals("PRIVATE")) {
  671. listObject.add(obj);
  672. }
  673. }
  674. }
  675. }
  676. for (JSONObject server: listObject) {
  677. String name = server.getString("Name");
  678. ConfigParser parser = new ConfigParser();
  679. parser.parseConfig(new InputStreamReader(getResources().openRawResource(R.raw.config)));
  680. VpnProfile vp = parser.convertProfile();
  681. vp.mName = name +"-"+server.getString("Category");
  682. vp.mConnections[0].mServerName = server.getString("IP");
  683. vp.mConnections[0].mServerPort = server.getString("Port");
  684. vp.mUseLzo = true;
  685.  
  686. getPM().saveProfile(this, vp);
  687. if (getPM().getProfileByName(name) == null) {
  688. getPM().addProfile(vp);
  689. }
  690. getPM().saveProfileList(this);
  691.  
  692. listServer.add(server);
  693. serverAdapter.notifyDataSetChanged();
  694. }
  695.  
  696. } catch (Exception e) {
  697. showToast("Loading Servers Error: " + e.getMessage());
  698. }
  699. }
  700. @Override
  701. protected void onActivityResult(int requestCode, int resultCode, Intent data)
  702. {
  703. switch (requestCode) {
  704. case 1:
  705. if (resultCode == RESULT_OK) {
  706. startVPN();
  707. }
  708. break;
  709. }
  710. // TODO: Implement this method
  711. super.onActivityResult(requestCode, resultCode, data);
  712. }
  713.  
  714. private void prepareToStart()
  715. {
  716. Intent intent = VpnService.prepare(this);
  717. if (intent != null) {
  718. startActivityForResult(intent, 1);
  719. } else {
  720. onActivityResult(1, RESULT_OK, null);
  721. }
  722. }
  723. private void stopVPN()
  724. {
  725. getPM().setConntectedVpnProfileDisconnected(this);
  726. stopVPN(connect_type.getCheckedRadioButtonId());
  727. StatisticGraphData.getStatisticData().getDataTransferStats().stop();
  728. graph.stop();
  729. enabledWidgets(true);
  730. }
  731.  
  732. private void startVPN()
  733. {
  734. try {
  735. ConfigUtil config = ConfigUtil.getInstance(this);
  736.  
  737.  
  738. JSONObject server = (JSONObject)server_spin.getSelectedItem();
  739. if (server != null) {
  740. String serverName = server.getString("Name");
  741. String category = server.getString("Category");
  742. VpnProfile vp = getPM().getProfileByName(serverName+"-"+category);
  743. vp.mUsername = username.getText().toString();
  744. vp.mPassword = password.getText().toString();
  745. vp.mUseDefaultRoute = true;
  746.  
  747. String mConnectRetry = "connect-retry 1";
  748. String mConnectRetryMax = "connect-retry-max 3";
  749. String mLocalProxy = String.format("http-proxy 127.0.0.1 %s", Integer.toString(ConfigUtil.LOCAL_PORT));
  750. vp.mConnections[0].mUseCustomConfig = true;
  751. vp.mConnections[0].mCustomConfiguration = String.format("%s\n%s\n%s\n", mConnectRetry, mConnectRetryMax, mLocalProxy);
  752.  
  753. getPM().saveProfile(this, vp);
  754.  
  755. config.setServerName(serverName + "-" + category);
  756. config.setSshPort(server.getString("SSHPort"));
  757. config.setSslPort(server.getString("SSLPort"));
  758. config.setIsCustomPayload(use_payload.isChecked());
  759. editor.putBoolean(USE_PAYLOAD + serverName, use_payload.isChecked());
  760. editor.apply();
  761.  
  762. startInjector(vp);
  763. enabledWidgets(false);
  764. } else {
  765. showToast("No Server Selected!");
  766. }
  767. } catch (Exception e) {
  768. showToast(e.getMessage());
  769. }
  770. }
  771. private JSONObject getSSLNetworkObject()
  772. {
  773. try {
  774. JSONObject sslObject = getSSLNetworksArray().getJSONObject(network_spin.getSelectedItemPosition());
  775. return sslObject;
  776. } catch (Exception e) {
  777. return null;
  778. }
  779. }
  780. @Override
  781. public void onNothingSelected(AdapterView<?> p1)
  782. {
  783. // TODO: Implement this method
  784. }
  785. @Override
  786. public void updateState(String state, String logmessage, int localizedResId, final ConnectionStatus level)
  787. {
  788. runOnUiThread(new Runnable() {
  789.  
  790. @Override
  791. public void run()
  792. {
  793. show_stats(VpnStatus.getLastCleanLogMessage(Main.this), level);
  794. // TODO: Implement this method
  795. }
  796.  
  797.  
  798. });
  799. // TODO: Implement this method
  800. }
  801.  
  802. @Override
  803. public void setConnectedVPN(String uuid)
  804. {
  805. // TODO: Implement this method
  806. }
  807.  
  808. @Override
  809. public void updateByteCount(long in, long out, long diffIn, long diffOut)
  810. {
  811. StatisticGraphData.DataTransferStats stats = StatisticGraphData.getStatisticData().getDataTransferStats();
  812. stats.addBytesReceived(in);
  813. stats.addBytesSent(out);
  814. DataTransferGraph.GraphData data = new DataTransferGraph.GraphData();
  815. data.setUp((int)out, 0);
  816. data.setDown((int)in, 0);
  817. // TODO: Implement this method
  818. }
  819. @Override
  820. public void updateState(int state)
  821. {
  822. if (state != 0) {
  823. status.setText(state);
  824. editor.putInt("SSH_STATUS", state).apply();
  825. }
  826. switch (state) {
  827. case R.string.state_connecting:
  828. case R.string.state_reconnecting:
  829. case R.string.state_auth:
  830. //status.setTextColor(Color.WHITE);
  831. enabledWidgets(false);
  832. showBanner(false);
  833. break;
  834. case R.string.state_connected:
  835. TastyToast.makeText(this, "Connected Success", TastyToast.LENGTH_SHORT,TastyToast.SUCCESS).show();
  836. //status.setTextColor(Color.GREEN);
  837. enabledWidgets(false);
  838. showBanner(false);
  839. break;
  840. case R.string.state_disconnected:
  841. case R.string.state_auth_failed:
  842. //status.setTextColor(Color.RED);
  843. StatisticGraphData.getStatisticData().getDataTransferStats().stop();
  844. graph.stop();
  845. enabledWidgets(true);
  846. showBanner(true);
  847. break;
  848. }
  849. // TODO: Implement this method
  850. }
  851. private void renderLastEvent()
  852. {
  853. int state = prefs.getInt("SSH_STATUS", R.string.state_disconnected);
  854. if (ConfigUtil.getInstance(this).getConnectionType() == 0) {
  855. status.setText(prefs.getString("OPENVPN_STATUS",""));
  856. } else {
  857. status.setText(state);
  858. switch (state) {
  859. case R.string.state_connecting:
  860. case R.string.state_reconnecting:
  861. case R.string.state_auth:
  862. enabledWidgets(false);
  863. showBanner(false);
  864. break;
  865. case R.string.state_connected:
  866. TastyToast.makeText(this, "Connected Success", TastyToast.LENGTH_SHORT,TastyToast.SUCCESS).show();
  867. enabledWidgets(false);
  868. showBanner(false);
  869. break;
  870. case R.string.state_disconnected:
  871. case R.string.state_auth_failed:
  872. enabledWidgets(true);
  873. StatisticGraphData.getStatisticData().getDataTransferStats().stop();
  874. graph.stop();
  875. showBanner(true);
  876. break;
  877. }
  878. }
  879.  
  880. }
  881.  
  882. private void showCustomPayload()
  883. {
  884. View v = getLayoutInflater().inflate(R.layout.custom_payload, null);
  885. custom_payload_input = (EditText)v.findViewById(R.id.custom_payload_input);
  886. final EditText custom_sni_input = (EditText)v.findViewById(R.id.custom_sni_input);
  887. final Button advance,cancel,save;
  888. advance = (Button)v.findViewById(R.id.custom_btn_advance);
  889. cancel = (Button)v.findViewById(R.id.custom_btn_cancel);
  890. save = (Button)v.findViewById(R.id.custom_btn_save);
  891.  
  892. final ConfigUtil config = ConfigUtil.getInstance(this);
  893. custom_payload_input.setText(prefs.getString("custom_payload_input", ""));
  894. custom_sni_input.setText(prefs.getString("custom_sni_input", ""));
  895.  
  896. final AlertDialog builder = new AlertDialog.Builder(this).create();
  897. builder.setTitle("Custom");
  898. builder.setView(v);
  899. advance.setOnClickListener(new OnClickListener() {
  900.  
  901. @Override
  902. public void onClick(View v)
  903. {
  904. try {
  905. JSONObject server = ((JSONObject)server_spin.getSelectedItem());
  906. String serverName = server.getString("Name");
  907. String category = server.getString("Category");
  908. VpnProfile vp = getPM().getProfileByName(serverName + "-" + category);
  909. gh = new GeneratorHelper(Main.this);
  910. gh.setCancelListener(Main.this);
  911. gh.setVpnProfile(vp);
  912. gh.show();
  913. builder.show();
  914. } catch (Exception e) {
  915. showToast(e.getMessage());
  916. }
  917. // TODO: Implement this method
  918. }
  919.  
  920.  
  921. });
  922. save.setOnClickListener(new OnClickListener() {
  923.  
  924. @Override
  925. public void onClick(View v)
  926. {
  927. String payload = custom_payload_input.getText().toString();
  928. String sni = custom_sni_input.getText().toString();
  929.  
  930. if (connect_type.getCheckedRadioButtonId() == R.id.type_ssh_ssl) {
  931. if (sni.isEmpty()) {
  932. showToast("SNI Host cannot be empty!");
  933. builder.show();
  934. } else {
  935. editor.putString("custom_sni_input", sni).apply();
  936. config.setSni(sni);
  937. builder.dismiss();
  938. }
  939. } else {
  940. if (payload.isEmpty()) {
  941. showToast("Payload is empty!");
  942. builder.show();
  943. } else {
  944. config.setPayload(payload);
  945. editor.putString("custom_payload_input",payload).apply();
  946. builder.dismiss();
  947. }
  948. }
  949. // TODO: Implement this method
  950. }
  951.  
  952.  
  953. });
  954. cancel.setOnClickListener(new OnClickListener() {
  955.  
  956. @Override
  957. public void onClick(View p1)
  958. {
  959. use_payload.setChecked(false);
  960. builder.dismiss();
  961. // TODO: Implement this method
  962. }
  963.  
  964.  
  965. });
  966. builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
  967.  
  968. @Override
  969. public void onCancel(DialogInterface p1)
  970. {
  971. use_payload.setChecked(false);
  972. // TODO: Implement this method
  973. }
  974.  
  975.  
  976. });
  977. builder.show();
  978. }
  979.  
  980. @Override
  981. public void onCancel(boolean z)
  982. {
  983. use_payload.setChecked(!z);
  984. network_spin.setEnabled(z);
  985. if (!z) {
  986. custom_payload_input.setText(ConfigUtil.getInstance(this).getPayload());
  987. }
  988. // TODO: Implement this method
  989. }
  990.  
  991. @Override
  992. public boolean onCreateOptionsMenu(Menu menu)
  993. {
  994. getMenuInflater().inflate(R.menu.home_menu, menu);
  995. // TODO: Implement this method
  996. return true;
  997. }
  998.  
  999. @Override
  1000. public boolean onOptionsItemSelected(MenuItem item)
  1001. {
  1002. switch (item.getItemId()) {
  1003. case R.id.menu_about:
  1004. about();
  1005. break;
  1006. }
  1007. // TODO: Implement this method
  1008. return super.onOptionsItemSelected(item);
  1009. }
  1010. public void viewWeb(String str)
  1011. {
  1012. if (str.isEmpty()) {
  1013. showToast("Link is empty!");
  1014. } else {
  1015. startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(str)));
  1016. }
  1017. }
  1018. public void about()
  1019. {
  1020. View v = getLayoutInflater().inflate(R.layout.about_app, null);
  1021. TextView tvDev = (TextView)v.findViewById(R.id.developer);
  1022. tvDev.setText("Tsholo Corporation");
  1023. int vis = tvDev.getVisibility();
  1024. if (vis == View.GONE) {
  1025. finish();
  1026. } else if (vis == View.INVISIBLE) {
  1027. finish();
  1028. }
  1029. AlertDialog.Builder ab = new AlertDialog.Builder(this);
  1030. ab.setTitle("About");
  1031. ab.setView(v);
  1032. ab.setPositiveButton("Ok", null);
  1033. ab.show();
  1034. }
  1035.  
  1036. /*private String getConfig()
  1037. {
  1038. try {
  1039.  
  1040. return str;
  1041. } catch (Exception e) {
  1042. showToast(e.getMessage());
  1043. }
  1044. return null;
  1045. }*/
  1046. private void startInjector(VpnProfile vp)
  1047. {
  1048. try {
  1049. ConfigUtil config = ConfigUtil.getInstance(this);
  1050. if (!use_payload.isChecked()) {
  1051. if (connect_type.getCheckedRadioButtonId() != R.id.type_ssh_ssl) {
  1052. JSONObject proxySet = ((JSONObject)server_spin.getSelectedItem()).getJSONObject("ProxySettings");
  1053. int position = network_spin.getSelectedItemPosition();
  1054. String payload = getNetworksArray().getJSONObject(position).getString("Payload");
  1055. String proxy = proxySet.getString("Squid");
  1056. String port = proxySet.getString("Port");
  1057.  
  1058. JSONObject sslObject = getSSLNetworkObject();
  1059. if (sslObject != null) {
  1060. String sni = sslObject.getString("SNIHost");
  1061. config.setSni(sni);
  1062. }
  1063. config.setPayload(payload);
  1064. config.setProxy(proxy);
  1065. config.setProxyPort(port);
  1066. }
  1067. }
  1068. } catch (Exception e) {
  1069. showToast("Start VPN Error: " + e.getMessage());
  1070. }
  1071. String user = username.getText().toString();
  1072. String pass = password.getText().toString();
  1073.  
  1074. if (user.isEmpty()) {
  1075. showToast("Username is empty!");
  1076. } else if (pass.isEmpty()) {
  1077. showToast("Password is empty!");
  1078. } else {
  1079. startService(new Intent(this, InjectorService.class).setAction("START").putExtra("UUID", vp.getUUIDString()));
  1080. }
  1081. }
  1082. private void show_stats(String log, ConnectionStatus level)
  1083. {
  1084. if (log != null) {
  1085. if (ConfigUtil.getInstance(this).getConnectionType() == 0) {
  1086. status.setText(log);
  1087. editor.putString("OPENVPN_STATUS", log).apply();
  1088. switch (level) {
  1089. case LEVEL_START:
  1090. case LEVEL_CONNECTING_NO_SERVER_REPLY_YET:
  1091. case LEVEL_CONNECTING_SERVER_REPLIED:
  1092. case LEVEL_VPNPAUSED:
  1093. //status.setTextColor(Color.WHITE);
  1094. enabledWidgets(false);
  1095. showBanner(false);
  1096. break;
  1097. case LEVEL_CONNECTED:
  1098. TastyToast.makeText(this, "Connected Success", TastyToast.LENGTH_SHORT,TastyToast.SUCCESS).show();
  1099. enabledWidgets(false);
  1100. showBanner(false);
  1101. break;
  1102. case LEVEL_NONETWORK:
  1103. enabledWidgets(false);
  1104. break;
  1105. case LEVEL_AUTH_FAILED:
  1106. TastyToast.makeText(this, "Wrong username or password!", TastyToast.LENGTH_SHORT,TastyToast.SUCCESS).show();
  1107. getPM().setConntectedVpnProfileDisconnected(this);
  1108. enabledWidgets(true);
  1109. showBanner(true);
  1110. break;
  1111. case LEVEL_NOTCONNECTED:
  1112. getPM().setConntectedVpnProfileDisconnected(this);
  1113. enabledWidgets(true);
  1114. showBanner(true);
  1115. break;
  1116. }
  1117. }
  1118. }
  1119. }
  1120. private void enabledWidgets(boolean z)
  1121. {
  1122.  
  1123. if (z) {
  1124. graphView.setVisibility(View.GONE);
  1125. connect.setVisibility(View.VISIBLE);
  1126. disconnect.setVisibility(View.GONE);
  1127. } else {
  1128. graphView.setVisibility(View.VISIBLE);
  1129. connect.setVisibility(View.GONE);
  1130. disconnect.setVisibility(View.VISIBLE);
  1131. }
  1132. for (int i = 0; i < connect_type.getChildCount(); i++) {
  1133. View v = connect_type.getChildAt(i);
  1134. v.setEnabled(z);
  1135. }
  1136. username.setEnabled(z);
  1137. password.setEnabled(z);
  1138. use_payload.setEnabled(z);
  1139. //save_pass.setEnabled(z);
  1140. bt_toggle_input.setEnabled(z);
  1141. server_spin.setEnabled(z);
  1142. network_spin.setEnabled(z);
  1143. category_spin.setEnabled(z);
  1144.  
  1145. }
  1146. private void showBanner(boolean z)
  1147. {
  1148. /*if (z) {
  1149. bannerView.setVisibility(View.VISIBLE);
  1150. graphView.setVisibility(View.GONE);
  1151. } else {
  1152. bannerView.setVisibility(View.GONE);
  1153. graphView.setVisibility(View.VISIBLE);
  1154. }*/
  1155. }
  1156. private ProfileManager getPM()
  1157. {
  1158. return ProfileManager.getInstance(this);
  1159. }
  1160.  
  1161. /*@Override
  1162. protected void onStart()
  1163. {
  1164. // TODO: Implement this method
  1165. super.onStart();
  1166. }*/
  1167.  
  1168. @Override
  1169. protected void onResume()
  1170. {
  1171. VpnStatus.addStateListener(this);
  1172. VpnStatus.addByteCountListener(this);
  1173. doBind();
  1174. if (InjectorService.isRunning) {
  1175. enabledWidgets(false);
  1176. } else {
  1177. enabledWidgets(true);
  1178. }
  1179. timer = new Timer();
  1180. timer.schedule(new TimerTask() {
  1181.  
  1182. @Override
  1183. public void run()
  1184. {
  1185. runOnUiThread(new Runnable() {
  1186.  
  1187. @Override
  1188. public void run()
  1189. {
  1190. StatisticGraphData.DataTransferStats stats = StatisticGraphData.getStatisticData().getDataTransferStats();
  1191. upBytes.setText(stats.byteCountToDisplaySize(stats.getBytesSent(), false));
  1192. dlBytes.setText(stats.byteCountToDisplaySize(stats.getBytesReceived(), false));
  1193. String duration = stats.isConnected() ? stats.elapsedTimeToDisplay(stats.getElapsedTime()) : "[00h:00m:00s]";
  1194. elapsedTime.setText(duration);
  1195. // TODO: Implement this method
  1196. }
  1197.  
  1198.  
  1199. });
  1200. // TODO: Implement this method
  1201. }
  1202.  
  1203.  
  1204. },0, 1000);
  1205. // TODO: Implement this method
  1206. super.onResume();
  1207. }
  1208.  
  1209. @Override
  1210. protected void onDestroy()
  1211. {
  1212. try {
  1213. doUnbind();
  1214. VpnStatus.removeStateListener(this);
  1215. VpnStatus.removeByteCountListener(this);
  1216.  
  1217. if (timer != null) {
  1218. timer.cancel();
  1219. }
  1220. } catch (Exception e) {
  1221. //ignore error we just want to remove listeners
  1222. }
  1223. // TODO: Implement this method
  1224. super.onDestroy();
  1225. }
  1226.  
  1227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement