Search in sources :

Example 1 with Request

use of com.wing.apirecord.core.model.Request in project apiRecord by tobecoder2015.

the class MethodFilter method doFilter.

@Override
public boolean doFilter(Message message, FilterChain chain) {
    if (message.getMsg() instanceof Request) {
        Request request = (Request) message.getMsg();
        String[] methods = pattern.split(",");
        boolean ok = true;
        for (String method : methods) {
            if (request.getMethod().equalsIgnoreCase(method)) {
                ok = false;
                break;
            }
        }
        if (!ok) {
            return chain.doFilter(message, chain);
        } else {
            log.debug("消息体不满足Method过滤器 " + request.getHost());
            return true;
        }
    }
    return chain.doFilter(message, chain);
}
Also used : Request(com.wing.apirecord.core.model.Request)

Example 2 with Request

use of com.wing.apirecord.core.model.Request in project apiRecord by tobecoder2015.

the class ApirecordApplication method main.

public static void main(String[] args) {
    SpringApplication.run(ApirecordApplication.class, args);
    System.setProperty("proxySet", "true");
    System.setProperty("http.proxyHost", "127.0.0.1");
    System.setProperty("http.proxyPort", "9999");
    log.info("本地代理服务器ip:" + System.getProperty("http.proxyHost"));
    log.info("本地代理服务器port:" + System.getProperty("http.proxyPort"));
    // 配置文件
    RecordQueue.getInstance().start();
    FilterChain.addFilter(new ContentTypeFilter(FilterService.contentType));
    FilterChain.addFilter(new UrlFilter(FilterService.urls));
    FilterChain.addFilter(new MethodFilter(FilterService.methods));
    FileService.setSavePath(null);
    new NettyHttpProxyServer().initProxyInterceptFactory(() -> new HttpProxyIntercept() {

        @Override
        public boolean beforeRequest(Channel clientChannel, HttpRequest httpRequest) {
            log.debug("request " + clientChannel.id().asShortText() + "   " + httpRequest.toString());
            Request request = TransferUtil.toRequest(httpRequest);
            Message message = new Message(clientChannel.id().asShortText(), Message.REQUEST_HEAD, request);
            FilterChain filterChain = FilterChain.getFilterChain();
            if (filterChain.doFilter(message, filterChain)) {
            // 
            } else {
                RecordQueue.getInstance().add(message);
            }
            return true;
        }

        @Override
        public boolean beforeRequest(Channel clientChannel, HttpContent httpContent) {
            log.debug("request httpContent:" + clientChannel.id().asShortText() + "   " + clientChannel.id().asShortText() + "  " + httpContent.content().toString(Charset.defaultCharset()));
            if (!RecordMap.containKey(clientChannel.id().asShortText()))
                return true;
            Message message = new Message(clientChannel.id().asShortText(), Message.REQUEST_BODY, httpContent.content().toString(io.netty.util.CharsetUtil.UTF_8));
            FilterChain filterChain = FilterChain.getFilterChain();
            if (filterChain.doFilter(message, filterChain)) {
            // 
            } else {
                RecordQueue.getInstance().add(message);
            }
            return true;
        }

        @Override
        public boolean afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse) {
            log.debug("httpResponse:" + clientChannel.id().asShortText() + "  " + httpResponse.toString());
            if (!RecordMap.containKey(clientChannel.id().asShortText()))
                return true;
            Response response = TransferUtil.toReponse(httpResponse);
            Message message = new Message(clientChannel.id().asShortText(), Message.RESPONSE_HEAD, response);
            FilterChain filterChain = FilterChain.getFilterChain();
            if (filterChain.doFilter(message, filterChain)) {
                message.setFilter(true);
                RecordQueue.getInstance().add(message);
            } else {
                RecordQueue.getInstance().add(message);
            }
            return true;
        }

        @Override
        public boolean afterResponse(Channel clientChannel, Channel proxyChannel, HttpContent httpContent) {
            log.debug("httpContent:" + clientChannel.id().asShortText() + "  " + httpContent.content().toString(Charset.defaultCharset()));
            if (!RecordMap.containKey(clientChannel.id().asShortText()))
                return true;
            ByteBuf buf = httpContent.content().copy();
            byte[] bytes = new byte[buf.readableBytes()];
            buf.readBytes(bytes);
            Message message = new Message(clientChannel.id().asShortText(), Message.RESPONSE_BODY, bytes);
            buf.writeBytes(bytes);
            if (httpContent instanceof LastHttpContent)
                message.setFinish(true);
            FilterChain filterChain = FilterChain.getFilterChain();
            if (filterChain.doFilter(message, filterChain)) {
            // 
            } else {
                RecordQueue.getInstance().add(message);
            }
            return true;
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            ctx.fireExceptionCaught(cause);
        // log.error("异常",cause);
        }
    }).start(9999);
}
Also used : Message(com.wing.apirecord.core.record.Message) MethodFilter(com.wing.apirecord.core.filter.MethodFilter) Channel(io.netty.channel.Channel) FilterChain(com.wing.apirecord.core.filter.FilterChain) Request(com.wing.apirecord.core.model.Request) UrlFilter(com.wing.apirecord.core.filter.UrlFilter) HttpProxyIntercept(com.wing.apirecord.core.intercept.HttpProxyIntercept) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) ContentTypeFilter(com.wing.apirecord.core.filter.ContentTypeFilter) NettyHttpProxyServer(com.wing.apirecord.core.NettyHttpProxyServer) Response(com.wing.apirecord.core.model.Response)

Example 3 with Request

use of com.wing.apirecord.core.model.Request in project apiRecord by tobecoder2015.

the class TransferUtil method toRequest.

public static Request toRequest(HttpRequest httpRequest) {
    Request request = new Request();
    request.setMethod(httpRequest.method().name());
    Iterator headers = httpRequest.headers().iteratorAsString();
    while (headers.hasNext()) {
        Map.Entry<String, String> header = (Map.Entry<String, String>) headers.next();
        request.addHeader(header.getKey(), header.getValue());
    }
    String uri = httpRequest.uri();
    request.setUrl(uri);
    int index = uri.indexOf("//");
    if (uri.indexOf("//") == -1) {
        return request;
    }
    index += 2;
    int hostindex = uri.indexOf("/", index);
    String host = uri.substring(index, hostindex);
    if (host != null) {
        host = host.trim();
        if (host.contains(":")) {
            request.setHost(host.split(":")[0]);
            request.setPort(Integer.parseInt(host.split(":")[1]));
        } else {
            request.setHost(host);
        }
    }
    String url = uri.substring(hostindex);
    if (url.contains("?")) {
        int splitindex = url.indexOf("?");
        request.setPath(url.substring(0, splitindex));
        String queryMapsLine = url.substring(splitindex + 1, url.length());
        String[] queryParaMaps = queryMapsLine.split("&");
        Map<String, String> queryParaMap = new HashMap<>();
        for (int i = 0; i < queryParaMaps.length; i++) {
            int parasplitindex = queryParaMaps[i].indexOf("=");
            if (parasplitindex < 0) {
                log.warn(queryParaMaps[i]);
                continue;
            }
            if (parasplitindex == queryParaMaps[i].length() - 1)
                queryParaMap.put(queryParaMaps[i].substring(0, parasplitindex), "");
            else
                queryParaMap.put(queryParaMaps[i].substring(0, parasplitindex), queryParaMaps[i].substring(parasplitindex + 1, queryParaMaps[i].length()));
        }
        request.setQueryParaMap(queryParaMap);
    } else {
        request.setPath(url);
    }
    return request;
}
Also used : HashMap(java.util.HashMap) Request(com.wing.apirecord.core.model.Request) HttpRequest(io.netty.handler.codec.http.HttpRequest) Iterator(java.util.Iterator) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with Request

use of com.wing.apirecord.core.model.Request in project apiRecord by tobecoder2015.

the class UrlFilter method doFilter.

@Override
public boolean doFilter(Message message, FilterChain chain) {
    if (message.getMsg() instanceof Request) {
        Request request = (Request) message.getMsg();
        String[] urls = pattern.split(",");
        // if (request.getHost().contains("127.0.0.1")||request.getHost().contains("localhost")) {
        // return true;
        // }
        boolean ok = true;
        for (String url : urls) {
            if (request.getUrl().contains(url)) {
                ok = false;
                break;
            }
        }
        if (!ok) {
            return chain.doFilter(message, chain);
        } else {
            log.debug("消息体不满足url过滤器 " + request.getHost());
            return true;
        }
    }
    return chain.doFilter(message, chain);
}
Also used : Request(com.wing.apirecord.core.model.Request)

Aggregations

Request (com.wing.apirecord.core.model.Request)4 NettyHttpProxyServer (com.wing.apirecord.core.NettyHttpProxyServer)1 ContentTypeFilter (com.wing.apirecord.core.filter.ContentTypeFilter)1 FilterChain (com.wing.apirecord.core.filter.FilterChain)1 MethodFilter (com.wing.apirecord.core.filter.MethodFilter)1 UrlFilter (com.wing.apirecord.core.filter.UrlFilter)1 HttpProxyIntercept (com.wing.apirecord.core.intercept.HttpProxyIntercept)1 Response (com.wing.apirecord.core.model.Response)1 Message (com.wing.apirecord.core.record.Message)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1