Протокол проверки вкр в системе Антиплагиат
Скачать 1.89 Mb.
|
74 public enum Command {run, start, reload, stop, set} private static volatile PowerManager.WakeLock wlInstance = null; private native long jni_init(int sdk); private native void jni_start(long context, int loglevel); private native void jni_run(long context, int tun, boolean fwd53, int rcode); private native void jni_stop(long context); private native void jni_clear(long context); private native int jni_get_mtu(); private native int[] jni_get_stats(long context); private static native void jni_pcap(String name, int record_size, int file_size); private native void jni_socks5(String addr, int port, String username, String password); private native void jni_done(long context); synchronized private static PowerManager.WakeLock getLock(Context context) { if (wlInstance == null) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); wlInstance = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, context.getString(R.string.app_name) + " wakelock"); wlInstance.setReferenceCounted(true); } return wlInstance; } synchronized private static void releaseLock(Context context) { if (wlInstance != null) { 75 while (wlInstance.isHeld()) wlInstance.release(); wlInstance = null; } } private final class CommandHandler extends Handler { public int queue = 0; public CommandHandler(Looper looper) { super(looper); } public void queue(Intent intent) { synchronized (this) { queue++; } Command cmd = (Command) intent.getSerializableExtra(EXTRA_COMMAND); Message msg = commandHandler.obtainMessage(); msg.obj = intent; msg.what = cmd.ordinal(); commandHandler.sendMessage(msg); } @Override public void handleMessage(Message msg) { 76 try { synchronized (MintferVpnService.this) { handleIntent((Intent) msg.obj); } } catch (Throwable ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); } finally { synchronized (this) { queue--; } } } private void handleIntent(Intent intent) { final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MintferVpnService.this); Command cmd = (Command) intent.getSerializableExtra(EXTRA_COMMAND); try { switch (cmd) { case start: start(); break; case stop: stop(temporarilyStopped); break; 77 default: break; } } catch (Throwable e) { e.printStackTrace() } } private void start() { if (vpn == null) { last_builder = getBuilder(); vpn = startVPN(last_builder); startNative(vpn); } } private void stop(boolean temporary) { if (vpn != null) { stopNative(vpn, true); stopVPN(vpn); vpn = null; } } private class StartFailedException extends IllegalStateException { public StartFailedException(String msg) { 78 super(msg); } } } @TargetApi(Build.VERSION_CODES.LOLLIPOP) private ParcelFileDescriptor startVPN(Builder builder) throws SecurityException { try { return builder.establish(); } catch (SecurityException ex) { throw ex; } catch (Throwable ex) { return null; } } private Builder getBuilder() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); // Build VPN service Builder builder = new Builder(); builder.setSession(getString(R.string.app_name)); // VPN address builder.addAddress(Constants.IPV4_ADDRESS, 32); builder.addAddress(Constants.IPV6_ADDRESS, 128); 79 // Routers builder.addRoute(Constants.IPV4_ROUTE, 0); builder.addRoute(Constants.IPV6_ROUTE, 3); // unicast // MTU int mtu = jni_get_mtu(); builder.setMtu(mtu); try { builder.addDisallowedApplication(getPackageName()); } catch (Exception e) { e.printStackTrace(); } return builder; } private void startNative(final ParcelFileDescriptor vpn) { jni_socks5("", 0, "", ""); if (tunnelThread == null) { jni_start(jni_context, Log.WARN); tunnelThread = new Thread(new Runnable() { @Override 80 public void run() { jni_run(jni_context, vpn.getFd(), mapForward.containsKey(53), 3); tunnelThread = null; } }); tunnelThread.start(); } } private void stopNative(ParcelFileDescriptor vpn, boolean clear) { if (tunnelThread != null) { jni_stop(jni_context); Thread thread = tunnelThread; while (thread != null) try { thread.join(); break; } catch (Exception e) { e.printStackTrace(); } tunnelThread = null; if (clear) jni_clear(jni_context); } } 81 private void stopVPN(ParcelFileDescriptor pfd) { Log.i(TAG, "Stopping"); try { pfd.close(); } catch (IOException ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); } } private void logPacket(Packet packet) { Message msg = packetHandler.obtainMessage(); msg.obj = packet; msg.what = MSG_PACKET; packetHandler.sendMessage(msg); } @Override public void onCreate() { Log.i(TAG, "Create version=" + Util.getSelfVersionName(this) + "/" + Util.getSelfVersionCode(this)); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); jni_context = jni_init(Build.VERSION.SDK_INT); boolean pcap = prefs.getBoolean("pcap", false); setPcap(pcap, this); 82 super.onCreate(); HandlerThread commandThread = new HandlerThread(getString(R.string.app_name) + " command", Process.THREAD_PRIORITY_FOREGROUND); HandlerThread packetThread = new HandlerThread(getString(R.string.app_name) + " log", Process.THREAD_PRIORITY_BACKGROUND); commandThread.start(); packetThread.start(); commandLooper = commandThread.getLooper(); packetLooper = packetThread.getLooper(); commandHandler = new CommandHandler(commandLooper); packetHandler = new PacketHandler(packetLooper); } @Override public int onStartCommand(Intent intent, int flags, int startId) { getLock(this).acquire(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); boolean enabled = prefs.getBoolean("enabled", false); 83 Command cmd = (Command) intent.getSerializableExtra(EXTRA_COMMAND); if (cmd == null) intent.putExtra(EXTRA_COMMAND, enabled ? Command.start : Command.stop); commandHandler.queue(intent); return START_STICKY; } @Override public void onDestroy() { synchronized (this) { Log.i(TAG, "Destroy"); commandLooper.quit(); packetLooper.quit(); for (Command command : Command.values()) commandHandler.removeMessages(command.ordinal()); releaseLock(this); try { if (vpn != null) { stopNative(vpn, true); stopVPN(vpn); vpn = null; } 84 } catch (Exception e) { e.printStackTrace(); } jni_done(jni_context); } super.onDestroy(); } private void showErrorNotification(String message) { Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); } private class Builder extends VpnService.Builder { private NetworkInfo networkInfo; private int mtu; private List } 85 @Override public VpnService.Builder setMtu(int mtu) { this.mtu = mtu; super.setMtu(mtu); return this; } @Override public Builder addAddress(String address, int prefixLength) { listAddress.add(address + "/" + prefixLength); super.addAddress(address, prefixLength); return this; } @Override public Builder addRoute(String address, int prefixLength) { listRoute.add(address + "/" + prefixLength); super.addRoute(address, prefixLength); return this; } @Override public Builder addDnsServer(InetAddress address) { listDns.add(address); super.addDnsServer(address); return this; } 86 @Override public Builder addDisallowedApplication(String packageName) throws PackageManager.NameNotFoundException { listDisallowed.add(packageName); super.addDisallowedApplication(packageName); return this; } } private class IPRule { private boolean block; private long expires; public IPRule(boolean block, long expires) { this.block = block; this.expires = expires; } public boolean isBlocked() { return this.block; } public boolean isExpired() { return System.currentTimeMillis() > this.expires; } 87 public void updateExpires(long expires) { this.expires = Math.max(this.expires, expires); } @Override public boolean equals(Object obj) { IPRule other = (IPRule) obj; return (this.block == other.block && this.expires == other.expires); } } public static void start(String reason, Context context) { Intent intent = new Intent(context, MintferVpnService.class); intent.putExtra(EXTRA_COMMAND, Command.start); intent.putExtra(EXTRA_REASON, reason); context.startService(intent); } public static void stop(String reason, Context context) { Intent intent = new Intent(context, MintferVpnService.class); intent.putExtra(EXTRA_COMMAND, Command.stop); intent.putExtra(EXTRA_REASON, reason); context.startService(intent); } } FragmentPackets.java public class FragmentPackets extends Fragment implements IPacketsView { 88 @Inject IPacketsPresenter presenter; private RecyclerView recyclerViewPackets; private TextView empty; private PacketsListAdapter adapter; List packetsList = new ArrayList<>(); public FragmentPackets() { } @Override public void onStart() { super.onStart(); presenter.registerEvent(); } @Override public void onDestroy() { presenter.unbindView(); super.onDestroy(); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { 89 super.onCreate(savedInstanceState); MainActivity.get(getActivity()).applicationComponent() .plus(new PacketsModule()).inject(this); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_packets, container, false); empty = (TextView) view.findViewById(R.id.empty_view); recyclerViewPackets = (RecyclerView) view.findViewById(R.id.list_packets); recyclerViewPackets.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity()); recyclerViewPackets.setLayoutManager(layoutManager); DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerViewPackets.getContext(), DividerItemDecoration.VERTICAL); recyclerViewPackets.addItemDecoration(dividerItemDecoration); isEmpty(); adapter = new PacketsListAdapter(getContext(), packetsList); recyclerViewPackets.setAdapter(adapter); presenter.bindView(this); 90 return view; } @Override public void onDestroyView() { presenter.unbindView(); super.onDestroyView(); } @Override public void onResume() { super.onResume(); PacketFilter packetFilter = ((MainActivity) getActivity()).getPacketFilter(); if (packetFilter != null) presenter.setPacketsFilter(packetFilter); } @Override public void updatePacketsListView(List packets) { this.packetsList = packets; adapter.setListPackets(packetsList); adapter.notifyDataSetChanged(); isEmpty(); } @Override public void showPacketDetails(Packet packet) { 91 // todo: maybe replace Bundle bundle = new Bundle(); bundle.putSerializable("packet", packet); FragmentDetails fragmentDetails = new FragmentDetails(); fragmentDetails.setArguments(bundle); FragmentManager manager = getFragmentManager(); manager.beginTransaction() .replace(R.id.container, fragmentDetails) .addToBackStack(null) .commit(); } private void isEmpty() { if (packetsList.isEmpty()) { recyclerViewPackets.setVisibility(View.GONE); empty.setVisibility(View.VISIBLE); } else { empty.setVisibility(View.GONE); recyclerViewPackets.setVisibility(View.VISIBLE); } } } IpacketsView.java 92 public interface IPacketsView { void updatePacketsListView(List packetList); void showPacketDetails(Packet packet); } PacketsPresenter.java public class PacketsPresenter implements IPacketsPresenter { private IPacketsView iPacketsView; private List listPackets = new ArrayList<>(); private PacketFilter packetsFilter; private Map } @Override public void bindView(IPacketsView iPacketsView) { this.iPacketsView = iPacketsView; } @Override public void unbindView() { iPacketsView = null; EventBus.getDefault().unregister(this); 93 } @Override public void registerEvent() { if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this); } @Override public void updatePacketsList(Packet packet) { packet.setApplicationInfo(installedApps.get(packet.uid)); if (packet.getApplicationInfo() != null) listPackets.add(packet); if (packetsFilter != null && packetsFilter.isFiltering()) { filterPackets(); } else { iPacketsView.updatePacketsListView(listPackets); } } @Subscribe public void onUpdatePacketsEvent(UpdatePacketsEvent event) { updatePacketsList(event.packet); } @Subscribe 94 public void onDetailsEvent(DetailsEvent event) { iPacketsView.showPacketDetails(event.packet); } @Subscribe public void onAppsInfo(AppInfosEvent event) { this.installedApps = event.installedApps; } @Subscribe public void onClearPacketsListEvent(ClearPacketListEvent event) { listPackets.clear(); iPacketsView.updatePacketsListView(listPackets); } @Override public void setPacketsFilter(PacketFilter packetsFilter) { this.packetsFilter = packetsFilter; if (packetsFilter.isFiltering()) { filterPackets(); } else { iPacketsView.updatePacketsListView(listPackets); } } 95 private void filterPackets() { List listFilteredPackets = FluentIterable.from(listPackets) .filter(predicateSource) .filter(predicateDestination) .filter(predicateProtocol).toList(); iPacketsView.updatePacketsListView(listFilteredPackets); } private Predicate predicateSource = new Predicate () { @Override public boolean apply(Packet packet) { String filterSourceAddress = packetsFilter.getSourceAddress(); String sourceAddress = packet.saddr; return (filterSourceAddress == null || filterSourceAddress.isEmpty()) || sourceAddress.equals(filterSourceAddress); } }; private Predicate predicateDestination = new Predicate () { @Override public boolean apply(Packet packet) { String filterDestinationAddress = packetsFilter.getDestinationAddress(); String destinationAddress = packet.daddr; return (filterDestinationAddress == null || filterDestinationAddress.isEmpty()) 96 || destinationAddress.equals(filterDestinationAddress); } }; private Predicate predicateProtocol = new Predicate () { @Override public boolean apply(Packet packet) { Constants.TransportProtocol packetProtocol = Constants.TransportProtocol.numberToEnum(packet.protocol); Constants.TransportProtocol filterProtocol = packetsFilter.getProtocol(); return (filterProtocol == null) || packetProtocol.equals(filterProtocol); } }; private Predicate predicateApp = new Predicate () { @Override public boolean apply(@Nullable Packet packet) { ApplicationInfo app = null; try { app = packet.getApplicationInfo(); } catch (Exception e) { e.printStackTrace(); } return app == null; 97 } }; } IpacketsPresenter.java public interface IPacketsPresenter { void bindView(IPacketsView iPacketsView); void unbindView(); void updatePacketsList(Packet packet); void registerEvent(); void setPacketsFilter(PacketFilter packetFilter); } FragmentFilters.java public class FragmentFilters extends Fragment implements IFiltersView { @Inject IFiltersPresenter presenter; public FragmentFilters() { } 98 @Override public void onStart() { super.onStart(); } @Override public void onStop() { super.onStop(); presenter.unbindView(); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainActivity.get(getActivity()).applicationComponent() .plus(new FiltersModule()).inject(this); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_filters, container, false); initView(view); presenter.bindView(this); 99 return view; } private void initView(View view) { EditText editTextSource = (EditText) view.findViewById(R.id.edit_filter_source); EditText editTextDestination = (EditText) view.findViewById(R.id.edit_filter_destination); ArrayAdapter getInstalledAppsInfo(getContext().getPackageManager())); |