All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fizzgate.plugin.auth.App Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/*
 *  Copyright (C) 2020 the original author or authors.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see .
 */

package com.fizzgate.plugin.auth;

import org.apache.commons.lang3.StringUtils;

import com.fizzgate.util.Consts;
import com.fizzgate.util.JacksonUtils;

import java.util.*;

/**
 * @author hongqiaowei
 */

public class App {

    public static final String ALL_APP = "*";

    static interface AUTH_TYPE {
        static final int SIGN       = 1;
        static final int CUSTOM     = 2;
        static final int SECRET_KEY = 3;
    }

    public boolean                     isDeleted                =  false;    // tb_app_auth.is_deleted

    public int                         id;                                   // tb_app_auth.id

    public String                      app;                                  // tb_app_auth.app

    public String                      name;                                 // tb_app_auth.app_name

    public boolean                     useAuth                  =  false;    // 0:false, 1:true

    public int                         authType;

    public String                      secretkey;

    public boolean                     useWhiteList             =  false;

    public String                      config;

    public Map> ips                      =  new HashMap<>();

    public void setDeleted(int v) {
        if (v == 1) {
            isDeleted = true;
        }
    }

    public void setUseAuth(int i) {
        if (i == AUTH_TYPE.SIGN || i == AUTH_TYPE.SECRET_KEY || i == AUTH_TYPE.CUSTOM) {
            useAuth = true;
        }
    }

    public void setUseWhiteList(int i) {
        if (i == 1) {
            useWhiteList = true;
        }
    }

    public void setIps(String ips) {
        if (StringUtils.isNotBlank(ips)) {
            Arrays.stream(StringUtils.split(ips, ',')).forEach(
                    ip -> {
                        ip = ip.trim();
                        int i = ip.lastIndexOf('.');
                        String subnet = ip.substring(0, i).trim();
                        String addrSeg = ip.substring(i + 1).trim();
                        if ("*".equals(addrSeg)) {
                            // this.ips.put(subnet, Collections.singletonList(new String[]{"1", "255"}));
                            List segs = this.ips.computeIfAbsent(subnet, k -> new ArrayList<>());
                            segs.add(new String[]{"1", "255"});
                        } else if (addrSeg.indexOf('-') > 0) {
                            String[] a = StringUtils.split(addrSeg, '-');
                            String beg = a[0].trim();
                            String end = a[1].trim();
                            List lst = this.ips.computeIfAbsent(subnet, k -> new ArrayList<>());
                            lst.add(new String[]{beg, end});
                        } else {
                            this.ips.put(ip, null);
                        }
                    }
            );
        }
    }

    public boolean allow(String ip) {
        if (ips.containsKey(ip)) {
            return true;
        }
        int originSubnetLen = ip.lastIndexOf(Consts.S.DOT);
        for (Map.Entry> e : ips.entrySet()) {
            String subnet = e.getKey();
            int subnetLen = subnet.length();
            byte i = 0;
            if (subnetLen == originSubnetLen) {
                for (; i < subnetLen; i++) {
                    if (subnet.charAt(i) != ip.charAt(i)) {
                        break;
                    }
                }
                if (i == subnetLen) {
                    int originAddrLen = ip.length() - originSubnetLen - 1;
                    boolean in = false;
                    for (String[] addrSeg : e.getValue()) {
                        in = inAddrSeg(ip, originSubnetLen, originAddrLen, addrSeg);
                        if (in) {
                            return in;
                        }
                    }
                    return in;
                }
            }
        }
        return false;
    }

    private boolean inAddrSeg(String ip, int originSubnetLen, int originAddrLen, String[] addrSeg) {
        String addrSegBeg = addrSeg[0];
        String addrSegEnd = addrSeg[1];
        if (originAddrLen < addrSegBeg.length() || addrSegEnd.length() < originAddrLen) {
            return false;
        } else {
            boolean b = true;
            if (originAddrLen == addrSegBeg.length()) {
                for (byte j = 0; j < addrSegBeg.length(); j++) {
                    char o = ip.charAt(originSubnetLen + 1 + j);
                    char a = addrSegBeg.charAt(j);
                    if (o < a) {
                        b = false;
                        break;
                    } else if (o > a) {
                        break;
                    }
                }
            }
            if (b) {
                if (originAddrLen == addrSegEnd.length()) {
                    for (byte j = 0; j < addrSegEnd.length(); j++) {
                        char a = addrSegEnd.charAt(j);
                        char o = ip.charAt(originSubnetLen + 1 + j);
                        if (a < o) {
                            b = false;
                            break;
                        } else if (a > o) {
                            break;
                        }
                    }
                }
            }
            return b;
        }
    }

    @Override
    public String toString() {
        return JacksonUtils.writeValueAsString(this);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy