精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>电脑技术>>● FreeBSD>>有待整理的文章-新加的文章都在这里>>有线宽带网GDVnet的自动认证

主题:有线宽带网GDVnet的自动认证
发信人: huanghwh(五云人)
整理人: hackerbay(2002-09-06 16:48:52), 站内信件
宽带网又搞用户认证了,而且只提供Web认证,搞得上网很不方便, 因为我从home到office做IPSec的VPN.
我写了一个简单的认证程序,可在系统起动时自动认证.不知freebsd下有多少人使用宽带网,
希望有所帮助.

package cn.gd.test;

import java.io.*;
import java.net.*;

/**
 * GDvnet have to use identify user before accessing Internet.
 *
 */

public class LoginGDvnet {
  HttpURLConnection conn = null;
  String userid = null;
  String password = null;

  String jsessionid = null;
  String preSessionid = null;
  String successLoc = null;
  String connectframe = null;

  public LoginGDvnet(String userid, String password) {
    this.userid = userid;
    this.password = password;
  }

  public void getJSessionID() throws IOException {
    //step 1. get jsessionid
    URL loginUrl = new URL("http://172.16.3.8/webcon/login.jsp");
    conn = (HttpURLConnection)loginUrl.openConnection();
    conn.setRequestProperty("User-Agent","al login gdvnet/0.00" );
    if( conn.getResponseCode()==HttpURLConnection.HTTP_OK) {
      jsessionid = conn.getHeaderField("Set-Cookie");
      int st = jsessionid.indexOf('=');
      int idx = jsessionid.indexOf(';');
      jsessionid = jsessionid.substring(st+1, idx).toLowerCase();
      System.err.println("Get jsessionid:\t"+jsessionid);
    }
    else {
      System.err.println("Could not pass 1, Exit...");
      System.exit(-1);
    }
    System.err.println("Step 1 pass.");

  }

  public void postU_P() throws IOException {
    //step 2. post userid and password
    URL processUrl = new URL("http://172.16.3.8/webcon/process.jsp;jsessionid="+jsessionid);
    conn = (HttpURLConnection)processUrl.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("User-Agent","al login gdvnet/0.00" );
    conn.setRequestProperty("Cookie", "JSESSIONID="+jsessionid);
    conn.setUseCaches(false);
    conn.setDoOutput(true);
    conn.setDoInput(true);

    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    String postData = "runscript=0&timeout=120&userid="+URLEncoder.encode(userid)+
                      "&passwd="+URLEncoder.encode(password);//+"\n";
    //System.err.println(postData);
    byteOut.write(postData.getBytes());
    byte[] buf = byteOut.toByteArray();

    conn.setRequestProperty("Content-type","application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-length",""+buf.length);
    conn.getOutputStream().write(buf);
    conn.getOutputStream().flush();
    conn.getOutputStream().close();
/*
    try {
      Thread.sleep(1*1000);
    }
    catch(Exception ex) {}
*/
    if( conn.getResponseCode()==HttpURLConnection.HTTP_OK) {
      /*
      BufferedReader brr = new BufferedReader( new java.io.InputStreamReader( conn.getInputStream()));
      String line = null;
      while( (line= brr.readLine()) != null) {
        System.out.println(line);
      }
      brr.close();*/
      System.err.println("Send userid and passwd...");
      System.err.println("Step 2 pass.");
    }
    else {
      System.err.println("Could not pass 2, Exit...");
      System.exit(-1);
    }
  }

  public void getSuccessURL()  throws IOException {
    //step 3. get successs location URL
    URL checkUrl = new URL("http://172.16.3.8/webcon/checklogin.jsp");
    conn = (HttpURLConnection)checkUrl.openConnection();
    conn.setInstanceFollowRedirects(false);
    conn.setRequestMethod("GET");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestProperty("User-Agent","Linyx/2.8 libwww-FM/2.14" );
    conn.setRequestProperty("Cookie", "JSESSIONID="+jsessionid);
    conn.setRequestProperty("Referer", "http://172.16.3.8/webcon/process.sjp;jsessionid="+jsessionid);
    if( conn.getResponseCode()==HttpURLConnection.HTTP_MOVED_TEMP) {
      successLoc = conn.getHeaderField("Location");
      System.err.println("Get success location :"+successLoc);
      System.err.println("Step 3 pass.");
    }
    else {
      System.err.println("Could not pass 3, Exit...");
      System.exit(-1);
    }

  }

  public void getPreSessionID() throws IOException {
    URL successUrl = new URL(successLoc);
    conn = (HttpURLConnection)successUrl.openConnection();
    conn.setDoInput(true);
    conn.setRequestProperty("User-Agent","lynx login gdvnet/0.00" );
    conn.setRequestProperty("Cookie", "JSESSIONID="+jsessionid);
    if( conn.getResponseCode()==HttpURLConnection.HTTP_OK) {
      //find sessionid
      BufferedReader brr = new BufferedReader( new java.io.InputStreamReader( conn.getInputStream()));
      String line = null;
      while( (line= brr.readLine()) != null) {
        if(line.indexOf("sessionid") != -1) {
          int st = line.lastIndexOf('=');
          //int st = line.indexOf('\"');
          int end = line.indexOf('>');
          preSessionid = line.substring(st+1,end);
          System.err.println("Get preSessionID:\t"+ preSessionid);
          System.err.println("Step 4 pass.");
          break;
        }
      }
      brr.close();
    }
    else {
      System.err.println("Could not pass 4, Exit...");
      System.exit(-1);
    }
  }

  public void disconnectUser() throws IOException {
    //step 2. post userid and password
    URL processUrl = new URL("http://172.16.3.8/webcon/disconnectuser.jsp");
    conn = (HttpURLConnection)processUrl.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("User-Agent","al login gdvnet/0.00" );
    conn.setRequestProperty("Cookie", "JSESSIONID="+jsessionid);
    conn.setUseCaches(false);
    conn.setDoOutput(true);
    conn.setDoInput(true);

    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    String postData = "userid="+URLEncoder.encode(userid)+
                      "&sessionid="+URLEncoder.encode(preSessionid);
    //System.err.println(postData);
    byteOut.write(postData.getBytes());
    byte[] buf = byteOut.toByteArray();

    conn.setRequestProperty("Content-type","application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-length",""+buf.length);
    conn.getOutputStream().write(buf);
    conn.getOutputStream().flush();
    conn.getOutputStream().close();
    if( conn.getResponseCode()==HttpURLConnection.HTTP_OK) {

      BufferedReader brr = new BufferedReader( new java.io.InputStreamReader( conn.getInputStream()));
      String line = null;
      while( (line= brr.readLine()) != null) {
        System.out.println(line);
      }
      brr.close();

      System.err.println("Send disconnect user...");
      System.err.println("Step 5 pass.");
    }
    else {
      System.err.println("Could not pass 5, Exit...");
      System.exit(-1);
    }

  }

  public void getRefreshURL() throws IOException {
    //step 4. get refresh URL
    URL successUrl = new URL(successLoc);
    conn = (HttpURLConnection)successUrl.openConnection();
    conn.setDoInput(true);
    conn.setRequestProperty("User-Agent","lynx login gdvnet/0.00" );
    conn.setRequestProperty("Cookie", "JSESSIONID="+jsessionid);
    if( conn.getResponseCode()==HttpURLConnection.HTTP_OK) {
      //find target=_blank
      BufferedReader brr = new BufferedReader( new java.io.InputStreamReader( conn.getInputStream()));
      String line = null;
      while( (line= brr.readLine()) != null) {
        if(line.startsWith("<a target=_blank")) {
int st = line.indexOf('?');
//int st = line.indexOf('\"');
int end = line.lastIndexOf('\"');
connectframe = "http://172.16.3.8/webcon/refreshtcp2.jsp?"+line.substring(st+1, end);
//connect3.jsp?
//connect2.jsp?
//refreshtcp2.jsp?
//refresh2.jsp?
System.err.println("Get Refresh URL:\t"+ connectframe);
System.err.println("Step 4 pass.");
break;
}
}
brr.close();
}
else {
System.err.println("Could not pass 4, Exit...");
System.exit(-1);
}
}

public void loopRefresh() throws IOException {

System.err.println("Now, Start refresh URL thread.");
while(true) {
try {
Thread.sleep(60*1000);
}
catch(Exception ex) {}
URL connectframeUrl = new URL(connectframe);
conn = (HttpURLConnection)connectframeUrl.openConnection();
conn.setDoInput(true);
conn.setRequestProperty("User-Agent","lynx login gdvnet/0.00" );
if( conn.getResponseCode()==HttpURLConnection.HTTP_OK) {
BufferedReader brr = new BufferedReader( new java.io.InputStreamReader( conn.getInputStream()));
String line = null;
boolean isLive = false;
while( (line= brr.readLine()) != null) {
if(line.startsWith("Keep Alive")) {
isLive = true;
System.out.println(line);
}
}
brr.close();
if(!isLive) {
break;
}
}
conn.disconnect();
}
}

public void process() {
while(true) {
try {
getJSessionID();
postU_P();
getSuccessURL();
if(successLoc.endsWith("notlogout.jsp")) {
getPreSessionID();
disconnectUser();
}
else {
getRefreshURL();
loopRefresh();
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}

public static void main(String[] args) throws Exception {
LoginGDvnet loginGDvnet1 = new LoginGDvnet("huhlht","lht1973");
loginGDvnet1.process();
}
}

[关闭][返回]