|
|
Строка 1: |
Строка 1: |
- | конфигурация типа шлюза :
| |
| | | |
- | <source lang="bash">
| |
- | user_rule.editor.class=bitel.billing.module.services.ipn.editor.ManadContractRuleEditor
| |
- | gate_manager.class=bitel.billing.server.ipn.ManadGateWorker
| |
- | use.script=1
| |
- | </source>
| |
- |
| |
- | Код :
| |
- | <source lang="java">
| |
- | import java.util.regex.Matcher;
| |
- | import java.util.regex.Pattern;
| |
- |
| |
- | import bitel.billing.common.module.ipn.IPNContractStatus;
| |
- | import bitel.billing.server.util.DefaultServerSetup;
| |
- | import bitel.billing.server.util.ssh.SSHSessionExec;
| |
- | import bitel.billing.server.ipn.bean.*;
| |
- |
| |
- | protected void doSync()
| |
- | {
| |
- |
| |
- | String host = gate.getHost();
| |
- | int port = gate.getPort();
| |
- |
| |
- |
| |
- | DefaultServerSetup gateSetup = new DefaultServerSetup( gate.getConfig(), "\r\n" );
| |
- |
| |
- | String login = gateSetup.getStringValue( "login", "root" );
| |
- |
| |
- | String pswd = gate.getKeyword();
| |
- |
| |
- | if( log.isDebugEnabled() )
| |
- | {
| |
- | log.debug( " gate: " + host + ":" + port + " login: " + login + " pswd: " + pswd );
| |
- | }
| |
- |
| |
- | SSHSessionExec session = null;
| |
- |
| |
- | try
| |
- | {
| |
- | session = new SSHSessionExec( host, port, login, pswd );
| |
- | //session.connect();
| |
- |
| |
- |
| |
- | for( status : statusList )
| |
- | {
| |
- | Integer cid = status.contractId;
| |
- |
| |
- | // правило для этого договора есть на шлюзе
| |
- | if ( ruleExists(cid, status, session) )
| |
- | {
| |
- | // если правило есть а юзер заблокирован - удаляем правило
| |
- | if ( status.status > 0 )
| |
- | {
| |
- | String[] rules = getCloseRules( status );
| |
- | for ( String rule : rules )
| |
- | {
| |
- | session.command( rule );
| |
- | }
| |
- |
| |
- | }
| |
- |
| |
- | }
| |
- |
| |
- | else if ( status.status == IPNContractStatus.STATUS_OPEN )
| |
- | {
| |
- | String[] rules = getOpenRules( status );
| |
- | for ( String rule : rules )
| |
- | {
| |
- | session.command( rule );
| |
- | }
| |
- |
| |
- | }
| |
- |
| |
- |
| |
- | }
| |
- |
| |
- | }
| |
- | catch ( Exception e )
| |
- | {
| |
- | throw new RuntimeException ( e );
| |
- | }
| |
- | finally
| |
- | {
| |
- | if( session != null )
| |
- | {
| |
- | session.disconnect();
| |
- | }
| |
- | }
| |
- |
| |
- | }
| |
- |
| |
- | private boolean ruleExists( cid, status, session ) throws Exception
| |
- | {
| |
- | //String answer = session.command( "ip firewall filter print" );
| |
- | String answer = session.command( "ip firewall address-list print" );
| |
- | return answer.indexOf( "!!" + cid + "!!" ) >= 0;
| |
- | }
| |
- |
| |
- |
| |
- | private String[] getOpenRules( status )
| |
- | {
| |
- |
| |
- | return getRules( status, "\\[OPEN\\]((.|\n)*)\\[/OPEN\\]" );
| |
- | }
| |
- |
| |
- | private String[] getCloseRules( status )
| |
- | {
| |
- |
| |
- |
| |
- | return getRules( status, "\\[CLOSE\\]((.|\n)*)\\[/CLOSE\\]" );
| |
- | }
| |
- |
| |
- |
| |
- |
| |
- |
| |
- | private String[] getRules( status, template )
| |
- | {
| |
- | // пользовательское правило, без типа - то все оставляем как есть
| |
- | String rule = status.rule.getRuleText();
| |
- |
| |
- |
| |
- |
| |
- | //типизированное правило
| |
- | if( status.ruleType != null )
| |
- |
| |
- | {
| |
- | String ruleText = ManadUtils.getRule( status.gateType, status.ruleType );
| |
- | rule = ManadUtils.generateRuleNew( ruleText, status.rule.getRuleText(), null, status.ruleType );
| |
- | }
| |
- |
| |
- |
| |
- | Pattern pattern = Pattern.compile( template );
| |
- | Matcher m = pattern.matcher( rule );
| |
- | if (m.find())
| |
- | {
| |
- | rule = m.group( 1 );
| |
- | }
| |
- |
| |
- | rule.replaceAll( "\r", "" );
| |
- |
| |
- |
| |
- | String[] parts = rule.split( "\n" );
| |
- |
| |
- |
| |
- | return parts;
| |
- | }
| |
- | </source>
| |