List of usage examples for io.netty.buffer ByteBuf clear
public abstract ByteBuf clear();
From source file:eu.operando.operandoapp.service.ProxyService.java
License:Open Source License
private HttpFiltersSource getFiltersSource() { return new HttpFiltersSourceAdapter() { @Override/*from w w w . j a v a2s.c om*/ public HttpFilters filterRequest(HttpRequest originalRequest) { return new HttpFiltersAdapter(originalRequest) { @Override public HttpObject serverToProxyResponse(HttpObject httpObject) { //check for proxy running if (MainUtil.isProxyPaused(mainContext)) return httpObject; if (httpObject instanceof HttpMessage) { HttpMessage response = (HttpMessage) httpObject; response.headers().set(HttpHeaderNames.CACHE_CONTROL, "no-cache, no-store, must-revalidate"); response.headers().set(HttpHeaderNames.PRAGMA, "no-cache"); response.headers().set(HttpHeaderNames.EXPIRES, "0"); } try { Method content = httpObject.getClass().getMethod("content"); if (content != null) { ByteBuf buf = (ByteBuf) content.invoke(httpObject); boolean flag = false; List<ResponseFilter> responseFilters = db.getAllResponseFilters(); if (responseFilters.size() > 0) { String contentStr = buf.toString(Charset.forName("UTF-8")); //Charset.forName(Charset.forName("UTF-8") for (ResponseFilter responseFilter : responseFilters) { String toReplace = responseFilter.getContent(); if (StringUtils.containsIgnoreCase(contentStr, toReplace)) { contentStr = contentStr.replaceAll("(?i)" + toReplace, StringUtils.leftPad("", toReplace.length(), '#')); flag = true; } } if (flag) { buf.clear().writeBytes(contentStr.getBytes(Charset.forName("UTF-8"))); } } } } catch (IndexOutOfBoundsException ex) { ex.printStackTrace(); Log.e("Exception", ex.getMessage()); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) { //ignore } return httpObject; } @Override public HttpResponse clientToProxyRequest(HttpObject httpObject) { //check for proxy running if (MainUtil.isProxyPaused(mainContext)) { return null; } //check for trusted access point String ssid = ((WifiManager) getSystemService(Context.WIFI_SERVICE)).getConnectionInfo() .getSSID(); String bssid = ((WifiManager) getSystemService(Context.WIFI_SERVICE)).getConnectionInfo() .getBSSID(); boolean trusted = false; TrustedAccessPoint curr_tap = new TrustedAccessPoint(ssid, bssid); for (TrustedAccessPoint tap : db.getAllTrustedAccessPoints()) { if (curr_tap.isEqual(tap)) { trusted = true; } } if (!trusted) { return getUntrustedGatewayResponse(); } //check for blocked url //check for exfiltration requestFilterUtil = new RequestFilterUtil(getApplicationContext()); locationInfo = requestFilterUtil.getLocationInfo(); contactsInfo = requestFilterUtil.getContactsInfo(); IMEI = requestFilterUtil.getIMEI(); phoneNumber = requestFilterUtil.getPhoneNumber(); subscriberID = requestFilterUtil.getSubscriberID(); carrierName = requestFilterUtil.getCarrierName(); androidID = requestFilterUtil.getAndroidID(); macAdresses = requestFilterUtil.getMacAddresses(); if (httpObject instanceof HttpMessage) { HttpMessage request = (HttpMessage) httpObject; if (request.headers().contains(CustomHeaderField)) { applicationInfo = request.headers().get(CustomHeaderField); request.headers().remove(CustomHeaderField); } if (request.headers().contains(HttpHeaderNames.ACCEPT_ENCODING)) { request.headers().remove(HttpHeaderNames.ACCEPT_ENCODING); } if (!ProxyUtils.isCONNECT(request) && request.headers().contains(HttpHeaderNames.HOST)) { String hostName = ((HttpRequest) request).uri(); //request.headers().get(HttpHeaderNames.HOST).toLowerCase(); if (db.isDomainBlocked(hostName)) return getBlockedHostResponse(hostName); } } String requestURI; Set<RequestFilterUtil.FilterType> exfiltrated = new HashSet<>(); if (httpObject instanceof HttpRequest) { HttpRequest request = (HttpRequest) httpObject; requestURI = request.uri(); try { requestURI = URLDecoder.decode(requestURI, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (locationInfo.length > 0) { //tolerate location miscalculation float latitude = Float.parseFloat(locationInfo[0]); float longitude = Float.parseFloat(locationInfo[1]); Matcher m = Pattern.compile("\\d+\\.\\d+").matcher(requestURI); List<String> floats_in_uri = new ArrayList(); while (m.find()) { floats_in_uri.add(m.group()); } for (String s : floats_in_uri) { if (Math.abs(Float.parseFloat(s) - latitude) < 0.5 || Math.abs(Float.parseFloat(s) - longitude) < 0.1) { exfiltrated.add(RequestFilterUtil.FilterType.LOCATION); } } } if (StringUtils.containsAny(requestURI, contactsInfo)) { exfiltrated.add(RequestFilterUtil.FilterType.CONTACTS); } if (StringUtils.containsAny(requestURI, macAdresses)) { exfiltrated.add(RequestFilterUtil.FilterType.MACADRESSES); } if (requestURI.contains(IMEI) && !IMEI.equals("")) { exfiltrated.add(RequestFilterUtil.FilterType.IMEI); } if (requestURI.contains(phoneNumber) && !phoneNumber.equals("")) { exfiltrated.add(RequestFilterUtil.FilterType.PHONENUMBER); } if (requestURI.contains(subscriberID) && !subscriberID.equals("")) { exfiltrated.add(RequestFilterUtil.FilterType.IMSI); } if (requestURI.contains(carrierName) && !carrierName.equals("")) { exfiltrated.add(RequestFilterUtil.FilterType.CARRIERNAME); } if (requestURI.contains(androidID) && !androidID.equals("")) { exfiltrated.add(RequestFilterUtil.FilterType.ANDROIDID); } } try { Method content = httpObject.getClass().getMethod("content"); if (content != null) { ByteBuf buf = (ByteBuf) content.invoke(httpObject); String contentStr = buf.toString(Charset.forName("UTF-8")); if (locationInfo.length > 0) { //tolerate location miscalculation float latitude = Float.parseFloat(locationInfo[0]); float longitude = Float.parseFloat(locationInfo[1]); Matcher m = Pattern.compile("\\d+\\.\\d+").matcher(contentStr); List<String> floats_in_uri = new ArrayList(); while (m.find()) { floats_in_uri.add(m.group()); } for (String s : floats_in_uri) { if (Math.abs(Float.parseFloat(s) - latitude) < 0.5 || Math.abs(Float.parseFloat(s) - longitude) < 0.1) { exfiltrated.add(RequestFilterUtil.FilterType.LOCATION); } } } if (StringUtils.containsAny(contentStr, contactsInfo)) { exfiltrated.add(RequestFilterUtil.FilterType.CONTACTS); } if (StringUtils.containsAny(contentStr, macAdresses)) { exfiltrated.add(RequestFilterUtil.FilterType.MACADRESSES); } if (contentStr.contains(IMEI) && !IMEI.equals("")) { exfiltrated.add(RequestFilterUtil.FilterType.IMEI); } if (contentStr.contains(phoneNumber) && !phoneNumber.equals("")) { exfiltrated.add(RequestFilterUtil.FilterType.PHONENUMBER); } if (contentStr.contains(subscriberID) && !subscriberID.equals("")) { exfiltrated.add(RequestFilterUtil.FilterType.IMSI); } if (contentStr.contains(carrierName) && !carrierName.equals("")) { exfiltrated.add(RequestFilterUtil.FilterType.CARRIERNAME); } if (contentStr.contains(androidID) && !androidID.equals("")) { exfiltrated.add(RequestFilterUtil.FilterType.ANDROIDID); } } } catch (IndexOutOfBoundsException ex) { ex.printStackTrace(); Log.e("Exception", ex.getMessage()); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) { //ignore } //check exfiltrated list if (!exfiltrated.isEmpty()) { //retrieve all blocked and allowed domains List<BlockedDomain> blocked = db.getAllBlockedDomains(); List<AllowedDomain> allowed = db.getAllAllowedDomains(); //get application name from app info String appName = applicationInfo.replaceAll("\\(.+?\\)", ""); //check blocked domains //if domain is stored as blocked, return a forbidden response for (BlockedDomain b_dmn : blocked) { if (b_dmn.info.equals(appName)) { return getForbiddenRequestResponse(applicationInfo, exfiltrated); } } //if domain is stored as allowed, return null for actual response for (AllowedDomain a_dmn : allowed) { if (a_dmn.info.equals(appName)) { return null; } } //get exfiltrated info to string array String[] exfiltrated_array = new String[exfiltrated.size()]; int i = 0; for (RequestFilterUtil.FilterType filter_type : exfiltrated) { exfiltrated_array[i] = filter_type.name(); i++; } //retrieve all pending notifications List<PendingNotification> pending = db.getAllPendingNotifications(); for (PendingNotification pending_notification : pending) { //if pending notification includes specific app name and app permissions return response that a pending notification exists if (pending_notification.app_info.equals(applicationInfo)) { return getPendingResponse(); } } //if none pending notification exists, display a new notification int notificationId = mainContext.getNotificationId(); mainContext.getNotificationUtil().displayExfiltratedNotification(getBaseContext(), applicationInfo, exfiltrated, notificationId); mainContext.setNotificationId(notificationId + 3); //and update statistics db.updateStatistics(exfiltrated); return getAwaitingResponse(); } return null; } }; } }; }
From source file:eu.operando.proxy.service.ProxyService.java
License:Open Source License
private HttpFiltersSource getFiltersSource() { return new HttpFiltersSourceAdapter() { // @Override // public int getMaximumRequestBufferSizeInBytes() { // // TODO Auto-generated method stub // return 10 * 1024 * 1024; ///*from w w w. j a v a 2 s . com*/ // } // // @Override // public int getMaximumResponseBufferSizeInBytes() { // // TODO Auto-generated method stub // return 10 * 1024 * 1024; // } @Override public HttpFilters filterRequest(HttpRequest originalRequest) { return new HttpFiltersAdapter(originalRequest) { @Override public HttpObject serverToProxyResponse(HttpObject httpObject) { if (MainUtil.isProxyPaused(mainContext)) return httpObject; if (httpObject instanceof HttpMessage) { HttpMessage response = (HttpMessage) httpObject; response.headers().set(HttpHeaderNames.CACHE_CONTROL, "no-cache, no-store, must-revalidate"); response.headers().set(HttpHeaderNames.PRAGMA, "no-cache"); response.headers().set(HttpHeaderNames.EXPIRES, "0"); } try { Method content = httpObject.getClass().getMethod("content"); if (content != null) { ByteBuf buf = (ByteBuf) content.invoke(httpObject); boolean flag = false; List<ResponseFilter> responseFilters = db.getAllResponseFilters(); if (responseFilters.size() > 0) { String contentStr = buf.toString(Charset.forName("UTF-8")); //Charset.forName(Charset.forName("UTF-8") for (ResponseFilter responseFilter : responseFilters) { String toReplace = responseFilter.getContent(); if (StringUtils.containsIgnoreCase(contentStr, toReplace)) { contentStr = contentStr.replaceAll("(?i)" + toReplace, StringUtils.leftPad("", toReplace.length(), '#')); flag = true; } } if (flag) { buf.clear().writeBytes(contentStr.getBytes(Charset.forName("UTF-8"))); } } } } catch (IndexOutOfBoundsException ex) { ex.printStackTrace(); Log.e("Exception", ex.getMessage()); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) { //ignore } return httpObject; } @Override public HttpResponse clientToProxyRequest(HttpObject httpObject) { if (MainUtil.isProxyPaused(mainContext)) return null; String requestURI; Set<RequestFilterUtil.FilterType> exfiltrated = new HashSet<>(); String[] locationInfo = requestFilterUtil.getLocationInfo(); String[] contactsInfo = requestFilterUtil.getContactsInfo(); String[] phoneInfo = requestFilterUtil.getPhoneInfo(); if (httpObject instanceof HttpMessage) { HttpMessage request = (HttpMessage) httpObject; if (request.headers().contains(CustomHeaderField)) { applicationInfoStr = request.headers().get(CustomHeaderField); request.headers().remove(CustomHeaderField); } if (request.headers().contains(HttpHeaderNames.ACCEPT_ENCODING)) { request.headers().remove(HttpHeaderNames.ACCEPT_ENCODING); } /* Sanitize Hosts */ if (!ProxyUtils.isCONNECT(request) && request.headers().contains(HttpHeaderNames.HOST)) { String hostName = request.headers().get(HttpHeaderNames.HOST).toLowerCase(); if (db.isDomainBlocked(hostName)) return getBlockedHostResponse(hostName); } } if (httpObject instanceof HttpRequest) { HttpRequest request = (HttpRequest) httpObject; requestURI = request.uri(); try { requestURI = URLDecoder.decode(requestURI, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } /* Request URI checks */ if (StringUtils.containsAny(requestURI, locationInfo)) { exfiltrated.add(RequestFilterUtil.FilterType.LOCATION); } if (StringUtils.containsAny(requestURI, contactsInfo)) { exfiltrated.add(RequestFilterUtil.FilterType.CONTACTS); } if (StringUtils.containsAny(requestURI, phoneInfo)) { exfiltrated.add(RequestFilterUtil.FilterType.PHONEINFO); } if (!exfiltrated.isEmpty()) { mainContext.getNotificationUtil().displayExfiltratedNotification(applicationInfoStr, exfiltrated); return getForbiddenRequestResponse(applicationInfoStr, exfiltrated); } } try { Method content = httpObject.getClass().getMethod("content"); if (content != null) { ByteBuf buf = (ByteBuf) content.invoke(httpObject); String contentStr = buf.toString(Charset.forName("UTF-8")); if (StringUtils.containsAny(contentStr, locationInfo)) { exfiltrated.add(RequestFilterUtil.FilterType.LOCATION); } if (StringUtils.containsAny(contentStr, contactsInfo)) { exfiltrated.add(RequestFilterUtil.FilterType.CONTACTS); } if (StringUtils.containsAny(contentStr, phoneInfo)) { exfiltrated.add(RequestFilterUtil.FilterType.PHONEINFO); } if (!exfiltrated.isEmpty()) { mainContext.getNotificationUtil() .displayExfiltratedNotification(applicationInfoStr, exfiltrated); return getForbiddenRequestResponse(applicationInfoStr, exfiltrated); } } } catch (IndexOutOfBoundsException ex) { ex.printStackTrace(); Log.e("Exception", ex.getMessage()); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) { //ignore } return null; } }; } }; }
From source file:impl.underdark.transport.bluetooth.discovery.ble.ManufacturerData.java
License:Open Source License
public byte[] build() { ByteBuf data = Unpooled.wrappedBuffer(new byte[27]); data.clear(); data.writeByte(1); // Version data.writeInt(appId);/*from www. ja va 2 s .c o m*/ data.writeBytes(address); int channelsMask = 0; for (int channel : channels) { if (channel < 0 || channel > BtUtils.channelNumberMax) continue; // http://www.vipan.com/htdocs/bitwisehelp.html int channelBit = ipow(2, channel); channelsMask |= channelBit; } data.writeInt(channelsMask); return data.array(); }
From source file:io.airlift.drift.transport.netty.server.ThriftProtocolDetection.java
License:Apache License
@Override protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) { // minimum bytes required to detect framing if (in.readableBytes() < 4) { return;/*from w w w . j a v a 2 s . c om*/ } int magic = in.getInt(in.readerIndex()); // HTTP not supported if (magic == HTTP_POST_MAGIC) { in.clear(); context.close(); return; } // Unframed transport magic starts with the high byte set, whereas framed and header // both start with the frame size which must be a positive int if ((magic & UNFRAMED_MESSAGE_MASK) == UNFRAMED_MESSAGE_FLAG) { Optional<Protocol> protocol = detectProtocol(magic); if (!protocol.isPresent()) { in.clear(); context.close(); return; } switchToTransport(context, UNFRAMED, protocol); return; } // The second int is used to determine if the transport is framed or header if (in.readableBytes() < 8) { return; } int magic2 = in.getInt(in.readerIndex() + 4); if ((magic2 & HEADER_MAGIC_MASK) == HEADER_MAGIC) { switchToTransport(context, HEADER, Optional.empty()); return; } Optional<Protocol> protocol = detectProtocol(magic2); if (!protocol.isPresent()) { in.clear(); context.close(); return; } switchToTransport(context, FRAMED, protocol); }
From source file:io.andromeda.logcollector.LocalFileReader.java
License:Apache License
private Observable.Operator<String, Buffer> extractLines() { ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(8192); return subscriber -> new Subscriber<Buffer>() { @Override// ww w . j av a2 s. c o m public void onCompleted() { if (!subscriber.isUnsubscribed()) { subscriber.onCompleted(); } } @Override public void onError(Throwable e) { if (!subscriber.isUnsubscribed()) { subscriber.onError(e); } } @Override public void onNext(Buffer buffer) { ByteBuf _buf = ((io.vertx.core.buffer.Buffer) buffer.getDelegate()).getByteBuf(); while (_buf.readableBytes() > 0) { byte b = _buf.readByte(); if ((b == '\n') || (b == '\r')) { byte[] _bArr = new byte[buf.readableBytes()]; buf.readBytes(_bArr); subscriber.onNext(new String(_bArr)); } else { buf.writeByte(b); } } buf.clear(); } }; }
From source file:io.aos.netty5.socksproxy.SocksPortUnificationServerHandler.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { ChannelPipeline p = ctx.pipeline();// w ww . j av a 2 s . com SocksProtocolVersion version = SocksProtocolVersion.valueOf(in.readByte()); System.out.println(version); in.resetReaderIndex(); switch (version) { case SOCKS4a: p.addLast(new Socks4CmdRequestDecoder()); p.addLast(Socks4MessageEncoder.INSTANCE); break; case SOCKS5: p.addLast(new Socks5InitRequestDecoder()); p.addLast(Socks5MessageEncoder.INSTANCE); break; case UNKNOWN: in.clear(); ctx.close(); return; } p.addLast(SocksServerHandler.INSTANCE); p.remove(this); }
From source file:io.hydramq.core.type.converters.FlagConverterTest.java
License:Open Source License
@Test public void testMarshalling() throws Exception { ConversionContext context = ConversionContext.base().register(PartitionFlags.Flag.class, new PartitionStateConverter()); ByteBuf buffer = Unpooled.buffer(); for (PartitionFlags.Flag expected : PartitionFlags.Flag.values()) { context.write(PartitionFlags.Flag.class, expected, buffer); PartitionFlags.Flag state = context.read(PartitionFlags.Flag.class, buffer); assertThat(state, is(expected)); buffer.clear(); }/* w w w .java 2 s. c o m*/ }
From source file:io.moquette.parser.netty.ConnAckDecoderTest.java
License:Open Source License
private void initHeader(ByteBuf buff) { buff.clear().writeByte(AbstractMessage.CONNACK << 4).writeByte(2); //reserved/*from ww w.j a va 2s . c o m*/ buff.writeByte((byte) 0); //return code buff.writeByte(ConnAckMessage.CONNECTION_ACCEPTED); }
From source file:io.moquette.parser.netty.ConnectDecoderTest.java
License:Open Source License
private void initHeader(ByteBuf buff, byte remaingLength) throws UnsupportedEncodingException { buff.clear().writeByte((byte) (AbstractMessage.CONNECT << 4)).writeByte(remaingLength); //Proto name/*from www . j a va 2 s . c o m*/ encodeString(buff, "MQIsdp"); //version buff.writeByte(3); //conn flags buff.writeByte(0xCE); //keepAlive buff.writeBytes(new byte[] { (byte) 0, (byte) 0x0A }); }
From source file:io.moquette.parser.netty.ConnectDecoderTest.java
License:Open Source License
private void initBaseHeader311_withFixedFlags(ByteBuf buff, byte fixedFlags, byte connectFlags) throws UnsupportedEncodingException { buff.clear().writeByte((byte) (AbstractMessage.CONNECT << 4) | fixedFlags).writeByte((byte) 0x0A); //Proto name//from w w w.j a va 2s . c o m encodeString(buff, "MQTT"); //version buff.writeByte(VERSION_3_1_1); //conn flags buff.writeByte(connectFlags); //keepAlive buff.writeBytes(new byte[] { (byte) 0, (byte) 0x0A }); }