Skip to content

Commit

Permalink
更新Target域名对象的字段
Browse files Browse the repository at this point in the history
  • Loading branch information
bit4woo committed Jul 12, 2024
1 parent 49deca2 commit c70aa93
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 44 deletions.
119 changes: 94 additions & 25 deletions src/dao/TargetDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public class TargetDao {
public TargetDao(String dbFilePath){
dataSource = DBUtils.getSqliteDataSource(dbFilePath);
jdbcTemplate = new JdbcTemplate(dataSource);
if (!testSelect()){
if (!tableExists("TargetTable")){
createTable();
}else {
alterTable();
}
}

Expand All @@ -38,32 +40,112 @@ public boolean createTable() {
" type TEXT NOT NULL,"+
" keyword TEXT NOT NULL,"+
" ZoneTransfer BOOLEAN NOT NULL,"+
" isBlack BOOLEAN NOT NULL,"+
//" isBlack BOOLEAN NOT NULL,"+
" trustLevel TEXT NOT NULL,"+
" comment TEXT NOT NULL,"+
" useTLD BOOLEAN NOT NULL)";
jdbcTemplate.execute(sqlTitle);


//https://www.sqlitetutorial.net/sqlite-replace-statement/
//让target字段成为replace操作判断是否重复的判断条件。
String sqlcreateIndex = "CREATE UNIQUE INDEX IF NOT EXISTS idx_Target_target ON TargetTable (target)";
jdbcTemplate.execute(sqlcreateIndex);
return true;
}

public void alterTable() {
removeColumnIsBlackIfPresent();
}

/**
*
* 实现:删除isBlack字段,新增trustLevel字段的逻辑。需要确保旧表有isBlack字段。
* @return
*/
public boolean removeColumnIsBlackIfPresent() {
if (!columnExists("TargetTable", "isBlack")) {
// 如果 isBlack 列不存在,直接返回
return true;
}

String createNewTableSql = "CREATE TABLE TargetTable_new (" +
"ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
"target TEXT NOT NULL, " +
"type TEXT NOT NULL, " +
"keyword TEXT NOT NULL, " +
"ZoneTransfer BOOLEAN NOT NULL, " +
"comment TEXT NOT NULL, " +
"useTLD BOOLEAN NOT NULL, " +
"trustLevel TEXT NOT NULL)";

String copyDataSql = "INSERT INTO TargetTable_new (ID, target, type, keyword, ZoneTransfer, comment, useTLD, trustLevel) " +
"SELECT ID, target, type, keyword, ZoneTransfer, comment, useTLD, " +
"CASE WHEN isBlack = 1 THEN 'NonTarget' ELSE 'Maybe' END as trustLevel " +
"FROM TargetTable";

String dropOldTableSql = "DROP TABLE TargetTable";

String renameTableSql = "ALTER TABLE TargetTable_new RENAME TO TargetTable";

try {
jdbcTemplate.execute(createNewTableSql);
jdbcTemplate.execute(copyDataSql);
jdbcTemplate.execute(dropOldTableSql);
jdbcTemplate.execute(renameTableSql);
return true;
} catch (DataAccessException e) {
e.printStackTrace();
return false;
}
}

private boolean columnExists(String tableName, String columnName) {
try {
String sql = "PRAGMA table_info(" + tableName + ")";
SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
while (rowSet.next()) {
if (columnName.equals(rowSet.getString("name"))) {
return true;
}
}
} catch (DataAccessException e) {
e.printStackTrace();
}
return false;
}

public boolean tableExists(String tableName){
try {
String sql = "SELECT count(*) FROM sqlite_master WHERE type='table' AND name = '"+tableName+"'";
SqlRowSet result = jdbcTemplate.queryForRowSet(sql);
if (result.next() && result.getInt(1) > 0) {
return true;
} else {
return false;
}
} catch (DataAccessException e) {
e.printStackTrace();
return false;
}
}


/**
* 不再使用isBlack字段,由trustLevel字段代替
* @param entry
* @return
*/
public boolean addOrUpdateTarget(TargetEntry entry){
String sql = "insert or replace into TargetTable (target,type,keyword,ZoneTransfer,isBlack,comment,useTLD)"
+ " values(?,?,?,?,?,?,?)";
public boolean addOrUpdateTarget(TargetEntry entry) {
String sql = "insert or replace into TargetTable (target, type, keyword, ZoneTransfer, comment, useTLD, trustLevel)"
+ " values(?, ?, ?, ?, ?, ?, ?)";

int result = jdbcTemplate.update(sql, entry.getTarget(),entry.getType(),entry.getKeyword(),entry.isZoneTransfer(),
entry.isBlack(),SetAndStr.toStr(entry.getComments()),entry.isUseTLD());
int result = jdbcTemplate.update(sql, entry.getTarget(), entry.getType(), entry.getKeyword(), entry.isZoneTransfer(),
SetAndStr.toStr(entry.getComments()), entry.isUseTLD(),
entry.getTrustLevel());

return result > 0;
return result > 0;
}


public boolean addOrUpdateTargets(List<TargetEntry> entries){
int num=0;
Expand Down Expand Up @@ -132,22 +214,9 @@ public TargetEntry selectByTarget(String target){
return null;
}
}

public boolean testSelect(){
try {
//String sql = "select * from TargetTable limit 1";
String sql = "SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'TargetTable'";
SqlRowSet result = jdbcTemplate.queryForRowSet(sql);
if (result.getRow() > 0 && result.getInt(0) > 0) {
return true;
}else {
return false;
}
} catch (DataAccessException e) {
e.printStackTrace();
return false;
}
}




/**
*
Expand Down
10 changes: 9 additions & 1 deletion src/dao/TargetMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.jdbc.core.RowMapper;

import base.SetAndStr;
import domain.target.AssetTrustLevel;
import domain.target.TargetEntry;

public class TargetMapper implements RowMapper<TargetEntry> {
Expand All @@ -25,7 +26,14 @@ public TargetEntry mapRow(ResultSet rs, int rowNum) throws SQLException {
entry.setType(rs.getString("type"));
entry.setKeyword(rs.getString("keyword"));
entry.setZoneTransfer(rs.getBoolean("ZoneTransfer"));
entry.setBlack(rs.getBoolean("isBlack"));
//entry.setBlack(rs.getBoolean("isBlack"));//反序列化时不再处理该字段。
if (rs.getString("trustLevel")!=null) {
entry.setTrustLevel(rs.getString("trustLevel"));
}else {
//兼容旧版本数据库
entry.setTrustLevel(AssetTrustLevel.Maybe);
}

if (rs.getString("comment").startsWith("[") && rs.getString("comment").endsWith("]")) {
//新的代码,setToStr的存储格式
entry.setComments(SetAndStr.toSet(rs.getString("comment")));
Expand Down
29 changes: 29 additions & 0 deletions src/domain/target/AssetTrustLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package domain.target;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AssetTrustLevel {

public static final String Maybe = "Maybe";
public static final String Confirm = "Confirm";
public static final String NonTarget = "NonTarget";
public static final String Cloud = "Cloud";

private static final String[] AssetTrustLevelArray = {Maybe,Confirm,NonTarget,Cloud};
private static List<String> AssetTrustLevelList = new ArrayList<>(Arrays.asList(AssetTrustLevelArray));

public static List<String> getLevelList(){
return AssetTrustLevelList;
}


public static String getNextLevel(String level) {
int index = AssetTrustLevelList.indexOf(level);
int nextIndex = (index + 1) % AssetTrustLevelList.size();
String nextTrustLevel = AssetTrustLevelList.get(nextIndex);
return nextTrustLevel;
}

}
2 changes: 1 addition & 1 deletion src/domain/target/TargetControlPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void selectedToBalck(){
TargetTableModel domainTableModel = domainPanel.fetchTargetModel();
for (int i = rowindexs.length - 1; i >= 0; i--) {
TargetEntry entry = domainTableModel.getValueAt(rowindexs[i]);
entry.setBlack(true);
entry.setTrustLevel(AssetTrustLevel.NonTarget);
domainTableModel.updateRow(entry);
}
}
Expand Down
29 changes: 28 additions & 1 deletion src/domain/target/TargetEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public class TargetEntry {
private String keyword = "";
private Set<String> AuthoritativeNameServers = new HashSet<>();//权威服务器
private boolean ZoneTransfer = false;//域名对应的权威服务器,是否存在域于传送漏洞
private boolean isBlack = false;//这个域名是否是黑名单根域名,需要排除的
private transient boolean isBlack = false;//这个域名是否是黑名单根域名,需要排除的。不再使用这个字段,由trustLevel代替
private Set<String> comments = new HashSet<>();
private boolean useTLD = true;//TLD= Top-Level Domain,比如 baidu.com为true,*.m.baidu.com为false
private String trustLevel = AssetTrustLevel.Maybe;

public static final String Target_Type_Domain = "Domain";
public static final String Target_Type_Wildcard_Domain = "WildcardDomain"; //
Expand All @@ -37,6 +38,7 @@ public class TargetEntry {

private static final String[] TargetTypeArray = {Target_Type_Domain,Target_Type_Wildcard_Domain,Target_Type_Subnet};
public static List<String> TargetTypeList = new ArrayList<>(Arrays.asList(TargetTypeArray));


public static void main(String[] args) {
TargetEntry aa = new TargetEntry("www.baidu.com");
Expand Down Expand Up @@ -132,12 +134,21 @@ public boolean isZoneTransfer() {
public void setZoneTransfer(boolean zoneTransfer) {
ZoneTransfer = zoneTransfer;
}

@Deprecated
public boolean isBlack() {
return isBlack;
}

@Deprecated
public void setBlack(boolean isBlack) {
this.isBlack = isBlack;
}

public boolean isNotTarget() {
return trustLevel.equalsIgnoreCase(AssetTrustLevel.NonTarget);
}

public Set<String> getComments() {
return comments;
}
Expand All @@ -159,6 +170,22 @@ public void setUseTLD(boolean useTLD) {
this.useTLD = useTLD;
}

public String getTrustLevel() {
if (isBlack) {
//兼容旧版本
trustLevel = AssetTrustLevel.NonTarget;
}
return trustLevel;
}

public void setTrustLevel(String trustLevel) {
this.trustLevel = trustLevel;
}

public String switchTrustLevel() {
return trustLevel = AssetTrustLevel.getNextLevel(trustLevel);
}

public void zoneTransferCheck() {
String rootDomain = InternetDomainName.from(target).topPrivateDomain().toString();
AuthoritativeNameServers = new HashSet<>(DomainUtils.GetAuthServer(rootDomain,null));
Expand Down
4 changes: 2 additions & 2 deletions src/domain/target/TargetTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public void mouseClicked(MouseEvent e) {
int modelCol = TargetTable.this.convertColumnIndexToModel(col);

TargetEntry selecteEntry = getTargetModel().getTargetEntries().get(rows[0]);
if (modelCol == TargetTableModel.getTitleList().indexOf("Black")) {
selecteEntry.setBlack(!selecteEntry.isBlack());
if (modelCol == TargetTableModel.getTitleList().indexOf("TrustLevel")) {
selecteEntry.switchTrustLevel();
guiMain.getDomainPanel().getTargetDao().addOrUpdateTarget(selecteEntry);
getTargetModel().fireTableRowsUpdated(rows[0], rows[0]);
} else if (modelCol == TargetTableModel.getTitleList().indexOf("Domain/Subnet")) {//双击url在浏览器中打开
Expand Down
Loading

0 comments on commit c70aa93

Please sign in to comment.